【发布时间】:2010-09-18 22:43:46
【问题描述】:
我有一个类似于以下的双向外来关系
class Parent(models.Model):
name = models.CharField(max_length=255)
favoritechild = models.ForeignKey("Child", blank=True, null=True)
class Child(models.Model):
name = models.CharField(max_length=255)
myparent = models.ForeignKey(Parent)
如何将 Parent.favoritechild 的选择限制为只有其父母是其自身的孩子?我试过了
class Parent(models.Model):
name = models.CharField(max_length=255)
favoritechild = models.ForeignKey("Child", blank=True, null=True, limit_choices_to = {"myparent": "self"})
但这会导致管理界面不列出任何子项。
【问题讨论】:
-
我认为你不应该使用“null=True”。在 django 文档中查找它
-
null=True 指的是 CharFields。在这里,设置 null=True 是完全可以的(否则没有孩子就无法保存父母)
标签: python django django-models