Opened 9 years ago

Closed 9 years ago

#25320 closed Bug (fixed)

ManyToManyField.null returns True on master (upcoming 1.9), False on 1.8.

Reported by: Anssi Kääriäinen Owned by: nobody
Component: Database layer (models, ORM) Version: dev
Severity: Release blocker Keywords:
Cc: tom@… Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Tom Christie)

According to report on PR https://github.com/django/django/pull/4241#issuecomment-135492016 we unintentionally changed the flag's value in 1.9. The null flag for ManyToManyField isn't that well specified to begin with (does it mean can the joins generate NULL values (True), or can the returned list be None (False)?). But I don't see any reason to change the flag's value in 1.9, so fix for the upcoming 1.9 release seems appropriate.

Change History (9)

comment:1 by Tom Christie, 9 years ago

Description: modified (diff)
Summary: ManyToManyField.null returns True on 1.8, False on 1.7.ManyToManyField.null returns True on master (upcoming 1.9), False on 1.8.

comment:2 by Tom Christie, 9 years ago

I've amended affected version on this ticket.

comment:3 by Tom Christie, 9 years ago

Severity: NormalRelease blocker
Version: 1.8master

comment:4 by Tom Christie, 9 years ago

Cc: tom@… added

comment:5 by Tim Graham, 9 years ago

If the flag is reverted then it seems we need to add back complicated logic in the admin list filters to get the tests passing. I'll take a closer look, but I thought maybe this would remind Anssi why he added the flags in the first place.

comment:6 by Tom Christie, 9 years ago

Cheap hack example implementation, if we did want to stick with null = False...

Have a private _to_many = True on these cases - then easy to get the same switching logic as is now used in master.
if field.null or getattr(field, '_to_many', False)

I assume there'd be nicer ways to get the right switching behavior, but just presenting this as a "it doesn't have to be difficult" example.

comment:7 by Tim Graham, 9 years ago

See the pull request for the test failures. If we remove ForeignObjectRel.null (it isn't present in 1.8) instead of changing it to False as done in the PR, we'll get this traceback:

======================================================================
ERROR: test_relatedfieldlistfilter_reverse_relationships (admin_filters.tests.ListFiltersTests)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/tim/code/django/tests/admin_filters/tests.py", line 478, in test_relatedfieldlistfilter_reverse_relationships
    changelist = self.get_changelist(request, User, modeladmin)
  File "/home/tim/code/django/tests/admin_filters/tests.py", line 252, in get_changelist
    modeladmin.list_select_related, modeladmin.list_per_page, modeladmin.list_max_show_all, modeladmin.list_editable, modeladmin)
  File "/home/tim/code/django/django/contrib/admin/views/main.py", line 78, in __init__
    self.queryset = self.get_queryset(request)
  File "/home/tim/code/django/django/contrib/admin/views/main.py", line 321, in get_queryset
    filters_use_distinct) = self.get_filters(request)
  File "/home/tim/code/django/django/contrib/admin/views/main.py", line 135, in get_filters
    if spec and spec.has_output():
  File "/home/tim/code/django/django/contrib/admin/filters.py", line 180, in has_output
    if self.field.null:
AttributeError: 'ManyToOneRel' object has no attribute 'null'

(Tom, I'll take a look at your suggestion -- composed this message and then received your response.)

comment:8 by Tim Graham, 9 years ago

Has patch: set

The test failures are because null=False hides the option to show "(None)" in the admin's list filter. Personally, I think null=True makes sense for the affected fields/relations (i.e. I wouldn't make any changes), but if we must revert, I've created a pull request using the approach suggested by Tom.

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

Resolution: fixed
Status: newclosed

In 064d4b1:

Fixed #25320 -- Reverted ManyToManyField.null to False for backwards compatibility.

Thanks Tom Christie for the report and review.

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