﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
18609	When using ModelAdmin.save_model, an existing instance is inserted again, instead of being updated	anonymous	nobody	"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()
}}}
"	Bug	closed	Uncategorized	1.3	Normal	invalid	modeladmin save_model		Unreviewed	0	0	0	0	0	0
