[Django] #27157: AdminForm model_admin cannot be None

3 views
Skip to first unread message

Django

unread,
Aug 31, 2016, 3:34:05 AM8/31/16
to django-...@googlegroups.com
#27157: AdminForm model_admin cannot be None
-------------------------------+--------------------
Reporter: tpazderka | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.10
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
When using `AdminForm` without the keyword argument `model_admin` which
defaults to None, we get `AttributeError` by calling
`get_empty_value_display` without checking for the `model_admin` type.
This seems to be also true for `AdminReadonlyField` class.

--
Ticket URL: <https://code.djangoproject.com/ticket/27157>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 31, 2016, 10:24:40 AM8/31/16
to django-...@googlegroups.com
#27157: AdminForm model_admin cannot be None
-------------------------------+--------------------------------------

Reporter: tpazderka | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------
Changes (by timgraham):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0


Comment:

Can you give some example code that causes this error? `AdminForm` isn't a
public API, and I'm not sure if you're calling it directly or triggering
the problem some other way.

--
Ticket URL: <https://code.djangoproject.com/ticket/27157#comment:1>

Django

unread,
Aug 31, 2016, 10:39:50 AM8/31/16
to django-...@googlegroups.com
#27157: AdminForm model_admin cannot be None
-------------------------------+--------------------------------------

Reporter: tpazderka | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------------------------

Comment (by tpazderka):

This is just a stub as our class is not based on `BaseFormset` but rather
reimplements its methods...
I am not sure if this is sufficient. If not, I will try to write more
complete example.

{{{
#!python
class ContactFormset(BaseFormset):
@property
def forms(self):
"""
Iterates through forms and returns them wrapped by `AdminForm`.
"""
for form, datatype in zip(self._forms, self.get_types()):
yield AdminForm(form, (self.get_fieldset(datatype), ), {},
self.get_readonly_fields(datatype))
}}}

The last line has to be replaced by:
{{{
#!python
yield AdminForm(form, (self.get_fieldset(datatype), ), {},
self.get_readonly_fields(datatype), model_admin=ModelAdmin(MojeidUser,
AdminSite))
}}}
in order to work in django 1.9 and later.

--
Ticket URL: <https://code.djangoproject.com/ticket/27157#comment:2>

Django

unread,
Aug 31, 2016, 7:51:44 PM8/31/16
to django-...@googlegroups.com
#27157: AdminForm may crash if model_admin argument is None
-------------------------------+------------------------------------

Reporter: tpazderka | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.10
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------+------------------------------------
Changes (by timgraham):

* stage: Unreviewed => Accepted


Comment:

I guess, for example, we need to account for `model_admin=None` in a place
like
[https://github.com/django/django/blob/d8ef5b0e6501692b8b767ebccddc936f496d77e8/django/contrib/admin/helpers.py#L185
AdminReadonlyField]. It's not so clear to me why `model_admin` is an
optional argument (see bcd9482a2019158f4580c24cd50ee8bfae9b2739), perhaps
for backwards compatibility. A better resolution might be a deprecation
path toward making those arguments required unless there's argument
against it. In your case, if you using that formset for the admin, then a
possibility would be to declare the formset in
`ModelAdmin.get_changelist_formset()` so you can access to the
`ModelAdmin` class (`self`).

--
Ticket URL: <https://code.djangoproject.com/ticket/27157#comment:3>

Reply all
Reply to author
Forward
0 new messages