Opened 8 years ago

Closed 8 years ago

Last modified 7 years ago

#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 Simon Charette, 8 years ago

Component: UncategorizedDatabase layer (models, ORM)
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug
Version: 1.9master

This was also brought up on django-users a couple of months ago.

comment:2 by Simon Charette, 8 years ago

Summary: default_related_name lookupRelatedField.related_query_name should default to _meta.default_related_name if defined.

comment:3 by Yi-Shan, Chen, 8 years ago

Owner: changed from nobody to Yi-Shan, Chen
Status: newassigned

I'll work on this in the next few days.

comment:5 by Tim Graham, 8 years ago

Patch needs improvement: set

The pull request is backwards-incompatible. It would be nice to have a deprecation path if possible.

comment:6 by Tim Graham <timograham@…>, 8 years ago

Resolution: fixed
Status: assignedclosed

In b84f5ab4:

Fixed #26230 -- Made default_related_name affect related_query_name.

comment:7 by Tim Graham <timograham@…>, 7 years ago

In bfe0d54:

Refs #26230 -- Removed support for model name query lookups when using Meta.default_related_name.

Per deprecation timeline.

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