[Fixed]-Django create new record instead of updating

1👍

You can set your primary key to None and Django automatically create new record. You can read more about this in docs.

def save(self, *args, **kwargs):
    if self.bar != self.__original_bar:
        self.FOOBAR_ID = None
        self.__original_bar= self.bar
    super(FooBar, self).save(*args, **kwargs)

0👍

You should remove the id when bar is being used. Try to remove it before sending the data to the front end.

What’s happening is that you are trying to add a new row to the database with a primary key “Key (“FOOBAR_ID”)=(2)” which already exists.

Leave a comment