因此,您意识到模型中的外键实际上应该是m2m,并且您已采用该实践来提供models.CASCADE作为外键的位置参数.所以你最终得到这个代码:
class Group(models.Model): members = models.ManyToManyField(Person, models.CASCADE)
你"修复"了一些其他的东西,突然结束了你试图做的所有事情的回溯尾巴:
File "/usr/local/lib/python3.5/site-packages/django/db/models/base.py", line 157, in __new__ new_class.add_to_class(obj_name, obj) File "/usr/local/lib/python3.5/site-packages/django/db/models/base.py", line 316, in add_to_class value.contribute_to_class(cls, name) File "/usr/local/lib/python3.5/site-packages/django/db/models/fields/related.py", line 1516, in contribute_to_class elif self.remote_field.is_hidden(): File "/usr/local/lib/python3.5/site-packages/django/db/models/fields/reverse_related.py", line 144, in is_hidden return bool(self.related_name) and self.related_name[-1] == '+' TypeError: 'function' object is not subscriptable
这是怎么回事?
ManyToManyField和ForeignKey的方法签名不相同.ManyToManyField第二个位置参数实际上是'related_name',这意味着您最终将models.CASCADE传递给related_name,如调试器中所示:
> /usr/local/lib/python3.5/site-packages/django/db/models/fields/related.py(1513)contribute_to_class() -> if self.remote_field.symmetrical and ( (Pdb) p self.remote_field.related_name
现在把它贴在你的显示器上,因为在3个月内你会再次使用它.