[Answer]-Changing primary key in a form creates two entries in DB, one with old primary key and other with new primary key

1👍

What you’re doing is updating private key. Django creates new instance in this case. It is is actually Django’s way to copy a record. The INSERT not UPDATE query is performed by ORM.

If you change your code to:

class Machines(models.Model):
    …
    ip_addr = models.CharField(max_length=50, unique=True, …
    …

Django will interprete your edit action correctly as the primary key won’t change.

Next time use django-debug-toolbar to see what query is performed by Django ORM.

Leave a comment