【发布时间】:2011-04-17 20:55:43
【问题描述】:
models.py:
class root(models.Model):
uid_string = models.CharField(max_length=255, unique=True)
class tree(models.Model):
uid_string = models.ForeignKey(root, to_field='uid_string', db_column='uid_string')
class shrub(models.Model):
uid_string = models.ForeignKey(root, to_field='uid_string')
显然,shrub 中的列将是 uid_string_id,而 tree 中的列将是 uid_string。 _id 附录被抑制。
如果我现在做一个
rootentry = root(uid_string = "text")
root.save()
我在执行以下查询时得到不同的行为:
>>> shrubentry = shrub(uid_string_id = rootentry.uid_string)
>>> treeentry = tree(uid_string = rootentry.uid_string)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File "/usr/local/lib/python2.6/site-packages/django/db/models/base.py", line 328, in __init__
setattr(self, field.name, rel_obj)
File "/usr/local/lib/python2.6/site-packages/django/db/models/fields/related.py", line 318, in __set__
self.field.name, self.field.rel.to._meta.object_name))
ValueError: Cannot assign "'text'": "tree.uid_string" must be a "root" instance.
>>>
显然rootentry.uid_string 是text
【问题讨论】:
标签: django django-models models