Opened 7 years ago

Closed 4 years ago

#27917 closed Cleanup/optimization (fixed)

Allow ModelAdmin.radio_fields to work with NullBooleanField

Reported by: Jerome Leclanche Owned by: Musen
Component: contrib.admin Version: 1.10
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

How to reproduce:

  • Create a model with a NullBooleanField "foo"
  • Create and register a corresponding ModelAdmin instance for the model
  • Add radio_fields = {"foo": admin.HORIZONTAL} to the model admin

It will be rejected with a SystemCheckError:

<class 'djstripe.admin.EventAdmin'>: (admin.E023) The value of 'radio_fields' refers to 'foo', which is not an instance of ForeignKey, and does not have a 'choices' definition.

This is very unintuitive. I'm unsure why even it *needs* a choices definition seeing as it has no problem rendering a <select> widget.

Relevant links:

Change History (6)

comment:1 by Tim Graham, 7 years ago

Component: Uncategorizedcontrib.admin
Summary: ModelAdmin rejects NullBooleanField as a valid radio_fields itemAllow ModelAdmin.radio_fields to work with NullBooleanField
Type: UncategorizedCleanup/optimization

It's not as simple as removing the system check. Changes in BaseModelAdmin.formfield_for_dbfield are also required to add support for NullBooleanField in radio_fields. Fixing #23130, which deprecates NullBooleanField in favor of allowing null=True on BooleanField, may help or even completely solve this issue.

comment:2 by Tim Graham, 7 years ago

Triage Stage: UnreviewedAccepted

comment:3 by Musen, 7 years ago

Owner: changed from nobody to Musen
Status: newassigned

comment:4 by Musen, 7 years ago

Tim, do you know the status of #23130? It seems to be inactive for 3 years. Do people come to the conclusion to deprecates NullBooleanField?

Also, I can see 2 ways to finish the ticket: one to hook NullBooleanField and create a ChoieField with (('1', 'Unknow'), ('2', ‘Yes'), ('3', 'No') instead (when it is in radio_fields); another is to create a NullBooleanRadioSelect widget to handle this like the original NullBooleanSelect widget. I am not which one is better or there could be an even better way.

comment:5 by Tim Graham, 7 years ago

Yes, I think we should try to deprecate NullBooleanField. I gave an update in ticket:23130#comment:20.

comment:6 by Jacob Walls, 4 years ago

Resolution: fixed
Status: assignedclosed

In d976c254fc76e5f04d81dfd9d142c58e933c9c92 the decision was made to document providing choices to forms.NullBooleanField in order to use Select or RadioSelect widgets. I believe that also resolves this issue, since the error message is now appropriate:

(admin.E023) The value of 'radio_fields' refers to 'foo', which is not an instance of ForeignKey, and does not have a 'choices' definition.

See discussion on tickets #23681 and #27445.

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