Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#27377 closed Cleanup/optimization (fixed)

Clarify that prepopulated_fields doesn't work with OneToOneField

Reported by: Malik A. Rumi Owned by: Henry Dang
Component: Documentation Version: 1.10
Severity: Normal Keywords: prepopulated_fields, admin.py
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

I don't know if this should be a bug report or a feature request.

I know that some variation on this question has been asked and answered many times on Stack Overflow. Usually, however, it is about using a foreign key. This is not my question. "prepopulated_fields doesn’t accept DateTimeField, ForeignKey, nor ManyToManyField fields." https://docs.djangoproject.com/en/1.10/ref/contrib/admin/#django.contrib.admin.ModelAdmin.prepopulated_fields

By implication, the docs are saying OneToOne fields are ok. This makes even more sense where the inherited models are a concrete subclass of a concrete base and we are really talking about a single object. For example, a Place can be a Restaurant, and a Restaurant can have a closed off, separate Bar where kids are not allowed. https://docs.djangoproject.com/en/1.10/topics/db/models/#one-to-one-relationships

However, admin.py is looking for a string to concatenate, not a filtered queryset leading to a related field or object. Furthermore, it expects that string to match the name of an existing field on the instant model, not some other model, even if they are the same object. How, then, can one use a OneToOne field or model in admin.py? I posted this to SO myself, and got ZERO responses. http://stackoverflow.com/questions/39778945/django-prepopulated-fields-inheritance

Yes, I know about model-utils and polymorphic. But if I am reading the docs correctly, this should be possible without resort to those tools. Besides, both are aimed primarily at queries, and say nothing directly about this specific issue with prepopulated fields in admin.py. Please advise. Thanks.

Change History (10)

comment:1 by Tim Graham, 8 years ago

Easy pickings: set
Summary: Prepopulated Fields in Django AdminClarify that prepopulated_fields doesn't work with OneToOneField
Triage Stage: UnreviewedAccepted
Type: BugCleanup/optimization

OneToOneField is a subclass of ForeignKey, so as far as I see, a system check error will occur. I guess the error message could be clarified since it confused you.

As far as getting answers to usage questions goes, please TicketClosingReasons/UseSupportChannels.

in reply to:  1 comment:2 by Malik A. Rumi, 8 years ago

Replying to Tim Graham:

OneToOneField is a subclass of ForeignKey, so as far as I see, a system check error will occur. I guess the error message could be clarified since it confused you.

As far as getting answers to usage questions goes, please TicketClosingReasons/UseSupportChannels.

Ok. Thx.

comment:3 by Sudheesh Singanamalla, 8 years ago

Owner: changed from nobody to Sudheesh Singanamalla
Status: newassigned

As far as what I understand from the discussion so far and the description, the fact that prepopulated_fields doesn't accept OneToOne fields needs to be explicitly mentioned in the documentation as well as an update to the following line https://github.com/django/django/blob/f734e2d4b2fc4391a4d097b80357724815c1d414/django/contrib/admin/checks.py#L402 mentioning

     else:
            if isinstance(field, (models.DateTimeField, models.ForeignKey, models.ManyToManyField)):
                return [
                    checks.Error(
                        "The value of '%s' refers to '%s', which must not be a DateTimeField, "
                        "a ForeignKey, a OneToOneField or a ManyToManyField." % (label, field_name),
                        obj=obj.__class__,
                        id='admin.E028',
                    )
                ]
            else:
                return []

Would this solve the issue ?

comment:4 by Tim Graham, 8 years ago

Yes, please keep the comma before "or" in the message. A test could also be added.

comment:5 by Malik A. Rumi, 8 years ago

Thanks guys. This will be a big help to all the literal minded people out there, like me.

comment:6 by Tim Graham, 8 years ago

Owner: Sudheesh Singanamalla removed
Status: assignednew

comment:7 by Henry Dang, 8 years ago

Owner: set to Henry Dang
Status: newassigned

comment:8 by Henry Dang, 8 years ago

Has patch: set

comment:9 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In 6af23a4:

Fixed #27377 -- Clarified that prepopulated_fields doesn't work with OneToOneField.

comment:10 by Tim Graham <timograham@…>, 8 years ago

In 12c1d6f:

Refs #27377 -- Fixed reverse query name clash in test model.

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