On Sun, 12 Oct 2014 22:01:51 -0700 (PDT)
dk <demi...@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? 
> 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?

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

-- 
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+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@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/20141013141555.422f4b4a%40jns42-l.w2k.keypro.fi.
For more options, visit https://groups.google.com/d/optout.

Reply via email to