I am a long time django user, first time bug reporter, so please let me
know if I need to do anything else to help get this bug fixed :)
I am using Django 3.1.3 and python 3.8.5 and have cerated a toy project to
illustrate the bug. I have the following models:
{{{
class UUIDModel(models.Model):
pkid = models.BigAutoField(primary_key=True, editable=False)
id = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)
class Meta:
abstract = True
class Thing(UUIDModel):
name = models.CharField(max_length=191)
class SubThing(models.Model):
name = models.CharField(max_length=191)
thing = models.ForeignKey(
'bugapp.Thing',
to_field='id',
on_delete = models.CASCADE,
related_name='subthings',
)
}}}
And the following admin.py file:
{{{
class SubThingInline(admin.StackedInline):
model = SubThing
@admin.register(Thing)
class ThingAdmin(admin.ModelAdmin):
list_display = ('name',)
ordering = ('pkid',)
#search_fields = ('name',)
inlines = (SubThingInline,)
}}}
When logging into the admin, if you delete all of the entries for
"subthings", add a name, and save the model, it will work. As soon as you
try to add a subthing alongside the main Thing, it fails with the
following exception:
It shows that the value of "id" in the Thing model is being set to null.
I believe this is a bug in django.
Thanks!
--
Ticket URL: <https://code.djangoproject.com/ticket/32210>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* Attachment "bugdemo.tbz" added.
I made a toy project containing all the code to reproduce the error and
zipped it.
Old description:
New description:
Hello,
inlines = (SubThingInline,)
}}}
When logging into the admin, if you delete all of the entries for
"subthings", add a name, and save the model, it will work. As soon as you
try to add a subthing alongside the main Thing, it fails with the
following exception:
It shows that the value of "id" in the Thing model is being set to null.
I believe this is a bug in django.
Thanks!
--
--
Ticket URL: <https://code.djangoproject.com/ticket/32210#comment:1>
* component: contrib.admin => Forms
* stage: Unreviewed => Accepted
Comment:
Thanks for the detailed report. I was able to reproduce this issue. It
looks that formsets' validation mutates the main object, because
`new_object.id` is not empty before the
[https://github.com/django/django/blob/fe9c7ded2996364f853c524b4421274717d89d5f/django/contrib/admin/options.py#L1584
all_valid()] call:
{{{
>>> new_object.id
e13fd82c-c3fc-42dc-ac12-577a4412ba51
>>> all_valid(formsets)
True
>>> new_object.id
None
}}}
Reproduced at ead37dfb580136cc27dbd487a1f1ad90c9235d15.
--
Ticket URL: <https://code.djangoproject.com/ticket/32210#comment:2>
* owner: nobody => Blackeyeforreal
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/32210#comment:3>
* owner: Blackeyeforreal => (none)
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/32210#comment:4>
Comment (by kamilturek):
Looks like it's not directly related to formsets' validation but
instantiating formset's forms.
{{{
(Pdb) new_object.id
UUID('06048350-3ad9-45f5-bca3-d08d795d7231')
(Pdb) formsets[0].forms
[<SubThingForm bound=True, valid=Unknown, fields=(name;thing;id;DELETE)>]
(Pdb) new_object.id
(Pdb)
}}}
{{{
(Pdb) new_object.id
UUID('652863a7-ccc8-4a18-9390-6fb77aa4bafa')
(Pdb) formsets[0]._construct_form(0)
<SubThingForm bound=True, valid=Unknown, fields=(name;thing;id;DELETE)>
(Pdb) new_object.id
(Pdb)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32210#comment:3>
* owner: nobody => (none)
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/32210#comment:4>
Comment (by NeErAj KuMaR):
The below code set id value **None** when formsets is_valid calling. so
need to stop set id value None when that field is not model's pk as
UUIDField.
{{{
if self.instance._state.adding:
if kwargs.get("to_field") is not None:
to_field =
self.instance._meta.get_field(kwargs["to_field"])
else:
to_field = self.instance._meta.pk
if to_field.has_default():
setattr(self.instance, to_field.attname, None)
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32210#comment:5>
* owner: (none) => NeErAj KuMaR
--
Ticket URL: <https://code.djangoproject.com/ticket/32210#comment:6>
* has_patch: 0 => 1
Comment:
Check PR for this issue.
[https://github.com/django/django/pull/15983]
--
Ticket URL: <https://code.djangoproject.com/ticket/32210#comment:7>
* cc: Neeraj Kumar (added)
--
Ticket URL: <https://code.djangoproject.com/ticket/32210#comment:8>
* cc: David Wobrock (added)
* needs_better_patch: 0 => 1
* needs_tests: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32210#comment:9>
* needs_better_patch: 1 => 0
* needs_tests: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/32210#comment:10>
* needs_tests: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32210#comment:11>
* needs_tests: 1 => 0
--
Ticket URL: <https://code.djangoproject.com/ticket/32210#comment:12>
* needs_tests: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32210#comment:13>
Comment (by Bhuvnesh):
[https://github.com/django/django/pull/16950 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/32210#comment:14>
* version: 3.1 => 4.2
Comment:
Any update about this issue?
Added PR related to this issue.
This issue still exists in the latest version of Django.
--
Ticket URL: <https://code.djangoproject.com/ticket/32210#comment:15>
* needs_tests: 1 => 0
Comment:
Resetting the PR flags to ensure the new PR gets picked up in the review
queue.
--
Ticket URL: <https://code.djangoproject.com/ticket/32210#comment:16>
* needs_better_patch: 0 => 1
* needs_tests: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/32210#comment:17>
* needs_better_patch: 1 => 0
* needs_tests: 1 => 0
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/32210#comment:18>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"eed096574fea5c9d82d0dc5952ad439dfde13718" eed0965]:
{{{
#!CommitTicketReference repository=""
revision="eed096574fea5c9d82d0dc5952ad439dfde13718"
Fixed #32210 -- Fixed model inlines with to_field that has a default.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/32210#comment:19>