Django

Code

Ticket #1305 (closed: fixed)

Opened 3 years ago

Last modified 2 years ago

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

Reported by: andreas@mindpicnic.com Assigned to: russellm
Milestone: Component: Core framework
Version: magic-removal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

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

django.reverse_lookup.patch (0.7 kB) - added by andreas@mindpicnic.com on 02/03/06 03:45:39.
reverse lookup by related name
reverse_lookup.unit_tests.py (1.7 kB) - added by andreas@mindpicnic.com on 02/05/06 04:42:06.
Reverse lookup unit tests

Change History

02/03/06 03:45:39 changed by andreas@mindpicnic.com

  • attachment django.reverse_lookup.patch added.

reverse lookup by related name

02/03/06 18:16:43 changed by russellm

  • owner changed from adrian to russellm.

Is this still a problem?

02/03/06 18:17:48 changed by adrian

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

02/03/06 18:24:47 changed 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.

02/05/06 04:42:06 changed by andreas@mindpicnic.com

  • attachment reverse_lookup.unit_tests.py added.

Reverse lookup unit tests

02/05/06 04:44:30 changed by andreas@mindpicnic.com

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).

02/06/06 05:21:06 changed by russellm

  • status changed from assigned to closed.
  • resolution set to fixed.

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


Add/Change #1305 ([magic-removal] Reverse foreign-key lookups should take related_name into account)




Change Properties
Action