#20742: ModelForm primary_key update with instance set is executed as INSERT caused by is_valid() chaning the instance -------------------------------------+------------------------------------- Reporter: amin@… | Owner: nobody Type: Bug | Status: new Component: Database layer | Version: 1.5 (models, ORM) | Keywords: modelform instance Severity: Normal | primary_key update Triage Stage: Unreviewed | Has patch: 0 Easy pickings: 0 | UI/UX: 0 -------------------------------------+------------------------------------- I do agree this is a bad database design but it's executed without any warning or anything in the docs so i want to open this ticket to allow the discussion of raising an Error or anything (e.g. an info in the docs @primary_key or modelform) not let anyone run into this mistake.
class Test(models.Model): email = models.CharField(max_length=100, primary_key=True) class TestForm(forms.ModelForm): class Meta: model = Test # assuming this object exists x = Test.objects.get(email='o...@example.com') form = TestForm(email='n...@example.com', instance=x) # here instance is updated with n...@example.com as pkey and django thinks it has to create a new model instance not updating the old instance if form.is_valid(): new_x = form.save() new_x.save() -- Ticket URL: <https://code.djangoproject.com/ticket/20742> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/055.9fa2d0ac06686886945f7db0dbd1e728%40djangoproject.com. For more options, visit https://groups.google.com/groups/opt_out.