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.
* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/20742#comment:1>
* 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>