Django Model Save and "Duplicate entry for key PRIMARY"

I have a django model, with two foreignkey filed to one model:

class MyModel(models.Model):
   user = models.ForeignKey(User)
   boss = models.ForeignKey(User, blank=True, null=True, related_name = 'the_boss')

   def save(self, *args, **kwargs)
       if self.pk:
          #do some staff
       super(MyModel, self).save(*args, **kwargs)

The boss field, add in model update. But when model updates (django admin) I have

Duplicate entry for key PRIMARY

super(MyModel, self).save(self, *args, **kwargs)

error. Any help with this issue will be appreciated.


尝试向用户ForeignKey添加一个related_name


Using related_name should work. Either the issue is on the #do some staff part or elsewhere like in model migrations.

链接地址: http://www.djcxy.com/p/54012.html

上一篇: Mac文本/代码编辑器

下一篇: Django模型保存和“重复键入主键”