#26230 closed Bug (fixed)
RelatedField.related_query_name should default to _meta.default_related_name if defined.
Reported by: | Daniel Bate | Owned by: | Yi-Shan, Chen |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | yes |
Easy pickings: | no | UI/UX: | no |
Description
If I attach a default_related_name
for my model using the name in a model lookup fails.
For example, if I have the following models:
from django.db import models class Foo(models.Model): a = models.IntegerField(default=0) class Bar(models.Model): foo = models.ForeignKey(Foo, related_name='bars') class Boo(models.Model): foo = models.ForeignKey(Foo) class Meta: default_related_name = 'boos'
And the following tests:
from django.test import TestCase from .models import Foo, Bar, Boo class Test(TestCase): def setUp(self): self.foo = Foo.objects.create() def test_related_name_on_field___all_is_fine(self): instance = Bar.objects.create(foo=self.foo) # get related set self.assertEqual(instance, self.foo.bars.first()) # filter foos through lookup self.assertEqual(self.foo, Foo.objects.get(bars=instance)) def test_related_name_is_default___lookup_fails(self): instance = Boo.objects.create(foo=self.foo) # get related set self.assertEqual(instance, self.foo.boos.first()) # filter foos through lookup self.assertEqual(self.foo, Foo.objects.get(boos=instance))
I get the following error:
django.core.exceptions.FieldError: Cannot resolve keyword 'boos' into field. Choices are: a, bars, boo, id
This shows I can use boos
as a property for Foo
objects but when i try to use it in a lookup it is expecting boo
rather than boos
Change History (7)
comment:1 by , 9 years ago
Component: | Uncategorized → Database layer (models, ORM) |
---|---|
Triage Stage: | Unreviewed → Accepted |
Type: | Uncategorized → Bug |
Version: | 1.9 → master |
comment:2 by , 9 years ago
Summary: | default_related_name lookup → RelatedField.related_query_name should default to _meta.default_related_name if defined. |
---|
comment:3 by , 9 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
I'll work on this in the next few days.
comment:5 by , 9 years ago
Patch needs improvement: | set |
---|
The pull request is backwards-incompatible. It would be nice to have a deprecation path if possible.
Note:
See TracTickets
for help on using tickets.
This was also brought up on django-users a couple of months ago.