我的模型中有一个带有递归m2m字段的类
我的model.py
:
class Person(models.Model):
name = models.CharField(_('name'), max_length=64)
slug = models.SlugField(primary_key=True)
friends = models.ManyToManyField('self',
related_name='nation_friends', symmetrical=False)
def save(self, *args, **kwargs):
self.slug = slugify(self.name)
super(Person, self).save(*args, **kwargs)
我用脚本填充数据库。当实例引用了尚未创建的实例时,就会出现问题
我的script.py
john = add_person('John', ['marc', 'philip'])
#john = add_person('John', [marc, philip]) #this gives immediately error: marc is not definited
marc = add_person('Marc', [])
philip = add_person('Philip', [])
def add_person(name, friends):
p=Person.objects.get_or_create(name=name)[0]
lista_friends = []
for f in friends:
lista_friends.append(f)
p.friends.set(lista_friends)
这是错误(我的翻译,在原文下面)
django.db.utils.IntegrityError: ERROR: the INSERT or the UPDATE on the
table "core_person_friends" break the constrain of external key "core_person_friends_to_person_id_ab3c358a_fk_core_person_slug"
DETAIL: The key (to_person_id)=(marc) is not present on the table "core_person".
原件:
django.db.utils.IntegrityError: ERRORE: la INSERT o l'UPDATE sulla
tabella "core_person_friends" viola il vincolo di chiave esterna "core_person_friends_to_person_id_ab3c358a_fk_core_person_slug"
DETAIL: La chiave (to_person_id)=(marc) non ?? presente nella tabella "core_person".
也许问题是因为子弹场
您应该传递对象列表,而不仅仅是值。按名称获取
Person
对象并添加这些对象。您正在尝试添加人名。还要确保姓名大小写相同,否则将为同一个人多次创建实例相关问题 更多 >
编程相关推荐