Opened 4 years ago

Closed 17 months ago

#32210 closed Bug (fixed)

Django Admin with Inlines not using UUIDField default value

Reported by: Joseph Metzinger Owned by: Neeraj Kumar
Component: Forms Version: 4.2
Severity: Normal Keywords:
Cc: Neeraj Kumar, David Wobrock Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Joseph Metzinger)

Hello,

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',)
    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:

https://dpaste.com/8EU4FF6RW

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!

Attachments (1)

bugdemo.tbz (10.1 KB ) - added by Joseph Metzinger 4 years ago.
I made a toy project containing all the code to reproduce the error and zipped it.

Download all attachments as: .zip

Change History (20)

by Joseph Metzinger, 4 years ago

Attachment: bugdemo.tbz added

I made a toy project containing all the code to reproduce the error and zipped it.

comment:1 by Joseph Metzinger, 4 years ago

Description: modified (diff)

comment:2 by Mariusz Felisiak, 4 years ago

Component: contrib.adminForms
Triage Stage: UnreviewedAccepted

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 all_valid() call:

>>> new_object.id
e13fd82c-c3fc-42dc-ac12-577a4412ba51
>>> all_valid(formsets)
True
>>> new_object.id
None

Reproduced at ead37dfb580136cc27dbd487a1f1ad90c9235d15.

comment:3 by Kamil Turek, 3 years ago

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) 

comment:4 by Neeraj Kumar, 2 years ago

Owner: nobody removed
Status: newassigned

comment:5 by Neeraj Kumar, 2 years ago

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)

comment:6 by Neeraj Kumar, 2 years ago

Owner: set to Neeraj Kumar

comment:7 by Neeraj Kumar, 2 years ago

Has patch: set

comment:8 by Neeraj Kumar, 2 years ago

Cc: Neeraj Kumar added

comment:9 by David Wobrock, 2 years ago

Cc: David Wobrock added
Needs tests: set
Patch needs improvement: set

comment:10 by Neeraj Kumar, 2 years ago

Needs tests: unset
Patch needs improvement: unset

comment:11 by Carlton Gibson, 2 years ago

Needs tests: set

comment:12 by Carlton Gibson, 2 years ago

Needs tests: unset

comment:13 by Carlton Gibson, 2 years ago

Needs tests: set

comment:15 by Neeraj Kumar, 17 months ago

Version: 3.14.2

Any update about this issue?

Added PR related to this issue.

This issue still exists in the latest version of Django.

comment:16 by Natalia Bidart, 17 months ago

Needs tests: unset

Resetting the PR flags to ensure the new PR gets picked up in the review queue.

comment:17 by Mariusz Felisiak, 17 months ago

Needs tests: set
Patch needs improvement: set

comment:18 by Mariusz Felisiak, 17 months ago

Needs tests: unset
Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:19 by Mariusz Felisiak <felisiak.mariusz@…>, 17 months ago

Resolution: fixed
Status: assignedclosed

In eed0965:

Fixed #32210 -- Fixed model inlines with to_field that has a default.

Note: See TracTickets for help on using tickets.
Back to Top