Code

Opened 2 years ago

Closed 4 months ago

#18668 closed New feature (fixed)

Make related_query_name more configurable

Reported by: zmsmith Owned by: nobody
Component: Database layer (models, ORM) Version: 1.4
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

Using related_name for accessor_name and related_query_name makes it difficult it mimic django's default functionality with overridden attribute names.

For example, if I have a model Foo with a ForeignKey to Bar, but I want Bar to interact with Foo as though it's a model named Fizz there's no way to allow Bar to have an attribute called fizz_set and query against the field fizz.

I believe this could be implemented easily and be backwards compatible by giving ForeignKey and ManyToManyField additional key word argument related_query_name that would be passed down to rel_class. Then get_query_name could just check for the existence of this value before returning either related_name or opts.object_name.lower() as it currently does.

Are there any reasons not to do this? I couldn't find any prior discussion in Trac.

Attachments (0)

Change History (4)

comment:1 Changed 20 months ago by aaugustin

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted

It took me quite some time to understand the problem described here, but I think it's valid.

I'll rephrase it to ensure we're talking about the same thing, and for the benefit of other contributors.

The OP's description translates to the following models:

from django.db import models

class Bar(models.Model):
    pass

class Foo(models.Model):
    name = models.CharField(max_length=100)
    bar = models.ForeignKey(Bar)

With these definitions, you can do:

bar.foo_set.all()
Bar.objects.filter(foo__name__contains='xxx')

Now, if you add related_name='fizz_set' to the ForeignKey declaration, you can do:

bar.fizz_set.all()
Bar.objects.filter(fizz_set__name__contains='xxx')
#                      ^^^^-- ugly!

And if you add related_name='fizz' it doesn't improve the situation:

bar.fizz.all()
#   ^^^^-- ugly!
Bar.objects.filter(fizz__name__contains='xxx')

comment:3 Changed 12 months ago by timo

  • Patch needs improvement set

Part of what's included in the PR above was committed in 99b467f272da91b8894dc90d793d8d2c40b78d8c and 12cb0df10f12e715bcaafbee4290c92d4ed6f111. It looks like the PR contains some things that weren't added (e.g. adding this on generic relations) but it needs to be updated to merge cleanly.

comment:4 Changed 4 months ago by loic84

  • Resolution set to fixed
  • Status changed from new to closed

#22207 [b77f26313cddbfde20dcf2661e9bd35458c2d1bd] fixed the remaining case of generic relations.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.