How to build this query

26 views
Skip to first unread message

ozgur yilmaz

unread,
Nov 9, 2012, 3:12:54 AM11/9/12
to django...@googlegroups.com
Hi all,

I hope i can explain my query problem well. Think you have 3 classes:

class City( models.Model ):
...
...

class Brand( models.Model ):
...
...

class Shop( models.Model ):
brand = models.ForeignKey( Brand )
city = models.ForeignKey( City )
...
...

class Discount( models.Model ):
brand = models.ForeignKey( Brand )
...
...

Think a Shop sell products of a single Brand. I have lots of Discount
records. But i'm interested in Discounts at Shops in Paris. Is there
any?

Can you help me?

Thanks,

Jirka Vejrazka

unread,
Nov 9, 2012, 3:33:07 AM11/9/12
to Django users
paris_discounts = Discount.objects.filter(brand__city__name='Paris')  ?

  Note that these are always *double* underscores and I had to make a guess that your City has an attribute "name".

  HTH

    Jirka




--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django...@googlegroups.com.
To unsubscribe from this group, send email to django-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


ozgur yilmaz

unread,
Nov 9, 2012, 3:37:01 AM11/9/12
to django...@googlegroups.com
But Brand doesnt have a city field. Shop class has a city field.

2012/11/9 Jirka Vejrazka <jirka.v...@gmail.com>:

ozgur yilmaz

unread,
Nov 9, 2012, 4:01:23 AM11/9/12
to django...@googlegroups.com
Maybe:
paris_discounts = Discount.objects.filter( brand__shop__city__name =
'Paris' ).distinct('id')

What do you think?

2012/11/9 ozgur yilmaz <yel...@gmail.com>:

Nikolas Stevenson-Molnar

unread,
Nov 9, 2012, 12:13:36 PM11/9/12
to django...@googlegroups.com
That looks correct, though you don't need distinct('id'), since
presumably id is your primary key and will already be unique.

_Nik
Reply all
Reply to author
Forward
0 new messages