我有一个模型,该模型具有到另一个模型的外键,并且当我尝试删除一个对象(在此示例中,存在的id == 0的对象)。
Models.py
class MyModel(models.Model):
example = models.ForeignKey(OtherModel,related_name='example',on_delete=models.SET_NULL ,blank=True,null=True)
class OtherModel(models.Model):
name = models.CharField(max_length=250)
外壳
>>> import project
>>> from project import models
>>> project.models.OtherModel.objects.get(id=0).delete()
Traceback (most recent call last):
File "console", line 1, in "module"
File ".../lib/python3.5/site-packages/django/db/models/base.py"
, line 890, in delete collector.collect([self], keep_parents=keep_parents)
File ".../lib/python3.5/site-packages/django/db/models/deletion.py", line 222, in collect
field.remote_field.on_delete(self, field, sub_objs, self.using)
TypeError: 'str' object is not callable
答案 0 :(得分:0)
您正在尝试使用尚未定义的模型创建外键关系。
example = models.ForeignKey(OtherModel,related_name='example',on_delete=models.SET_NULL ,blank=True,null=True)
此处在此行下方定义了其他模型,因此无法识别出其他模型。