#26230: default_related_name lookup
-------------------------------+--------------------
Reporter: OmegaDroid | Owner: nobody
Type: Uncategorized | Status: new
Component: Uncategorized | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------+--------------------
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`
--
Ticket URL: <https://code.djangoproject.com/ticket/26230>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/053.adf8b5d2776cf398db48944fa677b0fe%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.