query join tables

36 views
Skip to first unread message

dk

unread,
Oct 13, 2014, 1:01:51 AM10/13/14
to django...@googlegroups.com
I have this 2 models

class Choice(models.Model):
    restaurant
= models.ForeignKey(Restaurant)
    person
= models.ForeignKey(Person)
    date
= models.DateField("time published")
    time
= models.TimeField("date published")

class Person(models.Model):
    name
= models.CharField(max_length=100)
    last_name
= models.CharField(max_length=100)
    email
= models.EmailField()

and I would like to be able to see check if in a certain date, a certain person does exist in the record.
I am currently querying the date first.

date_query = Choice.objects.filter(date=dater)
for i in date_query:
    if i.person_id == to_email_i_am_looking:

might be another way to chain the querys?
kinda

SELECT person  FROM choice JOIN people
ON choice.person_ID=person+ID;
WHERE date= today and person=someone
?

I read about select_related that use the ForeignKey,  but I have 2 in my case,  the person and the restaurant,  does it loop on bouth?


Tom Lockhart

unread,
Oct 13, 2014, 1:17:51 AM10/13/14
to django...@googlegroups.com
On Oct 12, 2014, at 10:01 PM, dk <dem...@gmail.com> wrote:

I have this 2 models

class Choice(models.Model):
    restaurant = models.ForeignKey(Restaurant)
    person = models.ForeignKey(Person)
    date = models.DateField("time published")
    time = models.TimeField("date published")

class Person(models.Model):
    name = models.CharField(max_length=100)
    last_name = models.CharField(max_length=100)
    email = models.EmailField()

and I would like to be able to see check if in a certain date, a certain person does exist in the record.
I am currently querying the date first.

date_query = Choice.objects.filter(date=dater)
for i in date_query:
    if i.person_id == to_email_i_am_looking:

might be another way to chain the querys?

You can chain together the filters:

person = Person.objects.get(name=person_name)
choices_for_person_on_date = Choice.objects.filter(date=dater).filter(person=person)

hth

- Tom

dk

unread,
Oct 13, 2014, 3:21:59 AM10/13/14
to django...@googlegroups.com
I will give a try,   how does django know that have to join  using double filter? or it just auto-magically knows behind the hood?
or filtering by another model/class django get the idea, and joins the tables?

Thanks Tom =)

Anderson

unread,
Oct 13, 2014, 6:07:41 AM10/13/14
to django...@googlegroups.com
HI there..

Looks like you already have the person ID , you don't need hit the database every time  to call Person object.
You can make faster only check for ID not Person object.

instead this:

person = Person.objects.get(name=person_name)
choices_for_person_on_date = Choice.objects.filter(date=dater).filter(person=person)


You can do this

choices_for_person_on_date = Choice.objects.get(date=dater,person_id = to_email_i_am_looking)

and if you want the joins


Choice.objects.get(date=dater,person_id = to_email_i_am_looking).select_related('person')



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/a481b35d-a1db-497c-a39d-2519213500a5%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Anderson Dias Borges
Senior Analyst Developer

Tu cumprirás o desejo do meu coração se eu Te buscar...
I can't see but I'll take my chances
To hear You call my name

Jani Tiainen

unread,
Oct 13, 2014, 7:17:48 AM10/13/14
to django...@googlegroups.com
You can leverage reverse relation managers rather easily:

If you do have a person available already you can do the following:

choices_for_person_on_date = person.choice_set.filter(date=dater)

--

Jani Tiainen
Reply all
Reply to author
Forward
0 new messages