[Django] #20742: ModelForm primary_key update with instance set is executed as INSERT caused by is_valid() chaning the instance

7 views
Skip to first unread message

Django

unread,
Jul 12, 2013, 7:42:07 AM7/12/13
to django-...@googlegroups.com
#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.

Django

unread,
Jul 12, 2013, 7:42:58 AM7/12/13
to django-...@googlegroups.com
#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: new
Component: Database layer | Version: 1.5
(models, ORM) | Resolution:
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 amin@…):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


--
Ticket URL: <https://code.djangoproject.com/ticket/20742#comment:1>

Django

unread,
Jul 18, 2013, 4:49:53 PM7/18/13
to django-...@googlegroups.com
#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='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()

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='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()
}}}

--

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>

Reply all
Reply to author
Forward
0 new messages