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.
--
Ticket URL: <https://code.djangoproject.com/ticket/27377>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* needs_better_patch: => 0
* needs_tests: => 0
* easy: 0 => 1
* needs_docs: => 0
* type: Bug => Cleanup/optimization
* stage: Unreviewed => Accepted
Comment:
`OneToOneField` is a subclass of `ForeignKey`, so as far as I see,
[https://github.com/django/django/blob/f734e2d4b2fc4391a4d097b80357724815c1d414/django/contrib/admin/checks.py#L399-L406
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
[wiki:TicketClosingReasons/UseSupportChannels].
--
Ticket URL: <https://code.djangoproject.com/ticket/27377#comment:1>
Comment (by Malik A. Rumi):
Replying to [comment:1 Tim Graham]:
> `OneToOneField` is a subclass of `ForeignKey`, so as far as I see,
[https://github.com/django/django/blob/f734e2d4b2fc4391a4d097b80357724815c1d414/django/contrib/admin/checks.py#L399-L406
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
[wiki:TicketClosingReasons/UseSupportChannels].
Ok. Thx.
--
Ticket URL: <https://code.djangoproject.com/ticket/27377#comment:2>
* owner: nobody => Sudheesh Singanamalla
* status: new => assigned
Comment:
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 ?
--
Ticket URL: <https://code.djangoproject.com/ticket/27377#comment:3>
Comment (by Tim Graham):
Yes, please keep the comma before "or" in the message. A test could also
be added.
--
Ticket URL: <https://code.djangoproject.com/ticket/27377#comment:4>
Comment (by Malik A. Rumi):
Thanks guys. This will be a big help to all the literal minded people out
there, like me.
--
Ticket URL: <https://code.djangoproject.com/ticket/27377#comment:5>
* owner: Sudheesh Singanamalla => (none)
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/27377#comment:6>
* owner: (none) => Henry Dang
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/27377#comment:7>
* has_patch: 0 => 1
Comment:
[https://github.com/django/django/pull/7710 PR]
--
Ticket URL: <https://code.djangoproject.com/ticket/27377#comment:8>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"6af23a4521fcf80aec7c5ee3988914423c7dfccd" 6af23a4]:
{{{
#!CommitTicketReference repository=""
revision="6af23a4521fcf80aec7c5ee3988914423c7dfccd"
Fixed #27377 -- Clarified that prepopulated_fields doesn't work with
OneToOneField.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27377#comment:9>
Comment (by Tim Graham <timograham@…>):
In [changeset:"12c1d6fe8f03f068f6010e3c8e0f441bf2567e7c" 12c1d6f]:
{{{
#!CommitTicketReference repository=""
revision="12c1d6fe8f03f068f6010e3c8e0f441bf2567e7c"
Refs #27377 -- Fixed reverse query name clash in test model.
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/27377#comment:10>