Opened 13 years ago
Closed 13 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 |
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()
Note:
See TracTickets
for help on using tickets.