Code

#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

Description

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

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

meaning that in Model.save, 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"
m.region_id=3
m.save()

m.nom="test_modified"
m.save() # no error

Bug?

models.py

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)

admin.py

class MagasinAdmin(admin.ModelAdmin):

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

Attachments (0)

Change History (1)

comment:1 Changed 22 months ago by anonymous

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.