#20742: ModelForm primary_key update with instance set is executed as INSERT
caused
by is_valid() changing the instance
-------------------------------------+-------------------------------------
Reporter: amin@… | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 1.5
(models, ORM) | Resolution: needsinfo
Severity: Normal | Triage Stage:
Keywords: modelform instance | Unreviewed
primary_key update | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by kmtracey):
* status: new => closed
* resolution: => needsinfo
Old description:
> 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='[email protected]')
>
> form = TestForm(email='[email protected]', instance=x)
>
> # here instance is updated with [email protected] 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()
New description:
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.
{{{
#!python
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='[email protected]')
form = TestForm(email='[email protected]', instance=x)
# here instance is updated with [email protected] 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()
}}}
--
Comment:
A concrete suggestion for what's wanted here would help, as it is I'm not
sure what's being asked for.
--
Ticket URL: <https://code.djangoproject.com/ticket/20742#comment:2>
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 [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/070.e541037aa97ffd8f8fa5f6a061027cce%40djangoproject.com.
For more options, visit https://groups.google.com/groups/opt_out.