Opened 11 years ago

Closed 11 years ago

Last modified 10 years ago

#1305 closed defect (fixed)

[magic-removal] Reverse foreign-key lookups should take related_name into account

Reported by: andreas@… Owned by: russellm
Component: Core (Other) Version: magic-removal
Severity: normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:


Currently reverse ForeignKey lookups are impossible for models which are related to other models more than once. Could this be fixed by using the related_name instead of the model's name for resolving the one-to-many lookup?

class Poll(models.Model):
    question = models.CharField(maxlength=200)

class Choice(models.Model):
    name = models.CharField(maxlength=100)
    poll = models.ForeignKey(Poll, related_name="choice")
    related_poll = models.ForeignKey(Poll, related_name="related_choice")
first_poll = Poll(question = "What's the first question?")
second_poll = Poll(question = "What's the second question?")
new_choice = Choice(
    poll = first_poll,
    related_poll = second_poll,
    name = "This is the answer."

# The current state:
    Poll.objects.get_object(choice__name__exact="This is the answer.")
except TypeError:
    # raises TypeError with "Cannot resolve keyword 'choice' into field"
    # because more than one field could be identified by "choice"

# How it could be (lookups by related_name):
Poll.objects.get_object(choice__name__exact="This is the answer.") # returns first poll
Poll.objects.get_object(related_choice__name__exact="This is the answer.") # returns second poll

On a related note: It seems inconsistent to me that two ForeignKeys related to the same model result in a reverse lookup exception but, e.g., a ForeignKey and a ManyToMany-Field related to the same model don't raise the "TypeError: Can't resolve keyword" exception (the ManyToManyField is used, the ForeignKey ignored).

Attachments (2)

django.reverse_lookup.patch (735 bytes) - added by andreas@… 11 years ago.
reverse lookup by related name (1.7 KB) - added by andreas@… 11 years ago.
Reverse lookup unit tests

Download all attachments as: .zip

Change History (7)

Changed 11 years ago by andreas@…

reverse lookup by related name

comment:1 Changed 11 years ago by russellm

  • Owner changed from adrian to russellm

Is this still a problem?

comment:2 Changed 11 years ago by adrian

Please submit a model unit test if this is still a problem.

comment:3 Changed 11 years ago by russellm

  • Status changed from new to assigned

(bother - hit submit by accident). Is this still a problem? Your patch is against r2137 - I think this got resolved in r2145.

Changed 11 years ago by andreas@…

Reverse lookup unit tests

comment:4 Changed 11 years ago by andreas@…

This seems to work in the current version of magic-removal, great! I have attached the unit tests that demonstrate that it works (and make sure it doesn't break).

comment:5 Changed 11 years ago by russellm

  • Resolution set to fixed
  • Status changed from assigned to closed

(In [2280]) magic-removal: Fixed #1305 -- Added new unit test group for reverse lookup queries over foreign keys. Thanks, Andreas.

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