Opened 6 years ago

Closed 6 years ago

#18609 closed Bug (invalid)

When using ModelAdmin.save_model, an existing instance is inserted again, instead of being updated

Reported by: anonymous Owned by: nobody
Component: Uncategorized Version: 1.3
Severity: Normal Keywords: modeladmin save_model
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


When I change an existing Magasin instance from the admin, I get an IntegrityError, eg:

"Duplicate entry '220' for key 'PRIMARY'"

meaning that in, the instance is duplicated, instead of being updated.

When I do it programmatically, it works as expected, and the instance is updated:

m = Magasin()

m.nom="test_modified" # no error


class Magasin(models.Model):
    nom = models.CharField(max_length=200)
    region = models.ForeignKey(Region, blank=True, null=True)

    def save(self, *args, **kwargs):
        if self.region_id is None:
            self.region_id = args[0]
        super(Magasin, self).save(*args, **kwargs)

class MagasinAdmin(admin.ModelAdmin):

    def save_model(self, request, obj, form, change):
        # more stuff (using request object)

Change History (1)

comment:1 Changed 6 years ago by anonymous

Resolution: invalid
Status: newclosed
Note: See TracTickets for help on using tickets.
Back to Top