Opened 4 months ago

Last modified 3 months ago

#31486 assigned Bug

Deprecate passing unsaved objects to related filters.

Reported by: Mapiarz Owned by: Hasan Ramezani
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords:
Cc: Simon Charette Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: yes
Easy pickings: no UI/UX: no

Description

Consider this filter:

Foo.objects.filter(related_obj=bar)

Where 'bar' is an unsaved object instance. In Django 1.11, this would always return an empty QuerySet (since no Foo object is related to unsaved 'bar'). In Django 2.0 through 2.2, this is equivalent to doing (which can return a non-empty QuerySet):

Foo.objects.filter(related_obj=None)

I found a somewhat related issue that touches on this subject: https://code.djangoproject.com/ticket/27985

My questions:

  1. What is the intended behaviour? In the aforementioned issue Simon Charette suggests that unsaved objects should be prevented from being used in related filters. I agree with that.
  2. Is this documented anywhere? I couldn't find anything. At the very least this should be documented somewhere.

Change History (4)

comment:1 Changed 4 months ago by Simon Charette

Summary: Related filters with unsaved objectsDeprecate passing unsaved objects to related filters.
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug
Version: 2.2master

Regarding this comment I still think that deprecating passing unsaved objects to related filters is worth doing so I'll accept this ticket on this basis.

Mapiarz, would you be interested in submitting a patch doing so? It might require a bit of adjustments in the suite but I think that warning on obj.pk is None is the way to go as it's less controversial than obj._state.adding.

comment:2 Changed 4 months ago by Hasan Ramezani

Has patch: set
Owner: changed from nobody to Hasan Ramezani
Status: newassigned
Last edited 4 months ago by felixxm (previous) (diff)

comment:3 Changed 4 months ago by felixxm

Cc: Simon Charette added
Patch needs improvement: set

comment:4 Changed 3 months ago by Hasan Ramezani

As I commented on PR:

I don't know how to distinguish between p2.choice_set.all() and Choice.objects.filter(poll=p2), It seems get_normalized_value receives the same (value and lhs) for these both cases.

I've also tried to implement it in RelatedLookupMixin.get_prep_lookup based on Mariusz comment on PR, but still same problem. the lhs and rhs have the same value in p2.choice_set.all() and Choice.objects.filter(poll=p2) cases.

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