Opened 18 years ago

Closed 18 years ago

Last modified 17 years ago

#1305 closed defect (fixed)

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

Reported by: andreas@… Owned by: Russell Keith-Magee
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: no UI/UX: no

Description

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?")
first_poll.save()
second_poll = Poll(question = "What's the second question?")
second_poll.save()
new_choice = Choice(
    poll = first_poll,
    related_poll = second_poll,
    name = "This is the answer."
)
new_choice.save()

# The current state:
try:
    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"
    pass

# 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@… 18 years ago.
reverse lookup by related name
reverse_lookup.unit_tests.py (1.7 KB ) - added by andreas@… 18 years ago.
Reverse lookup unit tests

Download all attachments as: .zip

Change History (7)

by andreas@…, 18 years ago

Attachment: django.reverse_lookup.patch added

reverse lookup by related name

comment:1 by Russell Keith-Magee, 18 years ago

Owner: changed from Adrian Holovaty to Russell Keith-Magee

Is this still a problem?

comment:2 by Adrian Holovaty, 18 years ago

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

comment:3 by Russell Keith-Magee, 18 years ago

Status: newassigned

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

by andreas@…, 18 years ago

Reverse lookup unit tests

comment:4 by andreas@…, 18 years ago

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 by Russell Keith-Magee, 18 years ago

Resolution: fixed
Status: assignedclosed

(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