Feature request: ForeignKey through parameter

353 views
Skip to first unread message

Roald de Vries

unread,
Oct 28, 2010, 3:54:53 AM10/28/10
to django-d...@googlegroups.com, Roald de Vries
Dear all,


I quite often reference foreign keys of foreign keys of foreign
keys... Wouldn't it be nice to have a 'through'-parameter for
ForeignKey's?


class A(Model):
b = ForeignKey('B')
c = ForeignKey('C', through='B', related_name='a_set')

class B(Model):
c = ForeignKey('C')


The advantages of a field A.c wrt a property A.c:
- it creates a reverse relation, so C.a_set becomes available
- c becomes queryable: A.objects.filter(c=x)


For 'through'-ing one more class you could use:

class Z(Model):
a = ForeignKey('A')
c = ForeignKey('C', through='A') # works because A.c exists

or:

class Z(Model):
a = ForeignKey('A')
c = ForeignKey('C', through='A__B') # works regardless of
whether A.c exists


I'm curious if other people would be interested in such a feature, or
maybe if something alike or better already exists, and whether there
are downsides that I miss.

Cheers, Roald

Javier Guerra Giraldez

unread,
Oct 28, 2010, 10:02:50 AM10/28/10
to django-d...@googlegroups.com, Roald de Vries
On Thu, Oct 28, 2010 at 2:54 AM, Roald de Vries <down...@gmail.com> wrote:
> I quite often reference foreign keys of foreign keys of foreign keys...
> Wouldn't it be nice to have a 'through'-parameter for ForeignKey's?
>
>
>    class A(Model):
>        b = ForeignKey('B')
>        c = ForeignKey('C', through='B', related_name='a_set')
>
>    class B(Model):
>        c = ForeignKey('C')

i'd love such a feature too, but i think a better syntax could be
something like:

class A(Model):
b = ForeignKey('B')

c = ForeignKey('B__c', related_name='a_set')

class B(Model):
c = ForeignKey('C')

where the second part of the reference is the name of the field ('c'
in this example), not the model class ('C')

--
Javier

Roald de Vries

unread,
Oct 28, 2010, 10:15:34 AM10/28/10
to django-d...@googlegroups.com

On first sight, I think I agree with you that the syntax is cleaner
like this, but I would choose for the through-parameter because it's
more consistent with the use of the through-parameter for
ManyToManyField.

Javier Guerra Giraldez

unread,
Oct 28, 2010, 4:21:00 PM10/28/10
to django-d...@googlegroups.com
On Thu, Oct 28, 2010 at 9:15 AM, Roald de Vries <down...@gmail.com> wrote:
> On first sight, I think I agree with you that the syntax is cleaner like
> this, but I would choose for the through-parameter because it's more
> consistent with the use of the through-parameter for ManyToManyField.

but what if B has more than one ForeignKey('C') fields?

--
Javier

Adrian Holovaty

unread,
Oct 28, 2010, 6:19:30 PM10/28/10
to django-d...@googlegroups.com
On Thu, Oct 28, 2010 at 2:54 AM, Roald de Vries <down...@gmail.com> wrote:
> I quite often reference foreign keys of foreign keys of foreign keys...
> Wouldn't it be nice to have a 'through'-parameter for ForeignKey's?
>
>    class A(Model):
>        b = ForeignKey('B')
>        c = ForeignKey('C', through='B', related_name='a_set')
>
>    class B(Model):
>        c = ForeignKey('C')
>
>
> The advantages of a field A.c wrt a property A.c:
> - it creates a reverse relation, so C.a_set becomes available
> - c becomes queryable: A.objects.filter(c=x)

Hi Roald,

Thanks for suggesting this. I don't think it's worth implementing,
though, because you can already query across multiple foreign-key
relationships like this:

A.objects.filter(b__c__exact=x)

As for making C.a_set available, I'd suggest just doing
A.objects.filter(b__c__exact=c_instance) in your view, and that'll get
the job done.

Adrian

Reply all
Reply to author
Forward
0 new messages