Django IntegretyError,尝试更改许多字段的值时

2024-10-03 17:15:09 发布

您现在位置:Python中文网/ 问答频道 /正文

型号.py

class BaseMedia(AbstractObject):
    staff = models.ManyToManyField('main.Profession', blank=True)
    # etc

class Profession(Id):
    profession = models.CharField(max_length=15)
    creator = models.ForeignKey('main.Creator', on_delete=models.CASCADE)
    character = models.ForeignKey('main.Character', on_delete=models.CASCADE, null=True, blank=True)

表单.py

for profession, staff in staff.items():
    for person in staff:
        professions.append(Profession.objects.update_or_create(
            profession=profession,
            creator_id=person.get('creator_id'),
            character_id=person.get('character_id')
        )[0])

instance.staff.set(professions)

instance.staff.set(professions)来自forms.pyraise:

django.db.utils.IntegrityError: update or delete on table "main_profession" violates 
foreign key constraint "main_basemedia_staff_profession_id_075458d2_fk_main_prof" on 
table "main_basemedia_staff"

DETAIL:  Key (id)=(dd032d79-d458-4ead-b05b-da3b419b58a0) is still referenced from table 
"main_basemedia_staff".

我完全不知道这是问题的关键


Tags: idtruemainonmodelstabledeleteperson