Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Dec. 1, 2010 15:07:39

c.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Unique querysets


Hi, I have a question about the ORM.

If I have model class's:

class Event(models.Model):
....

class Ticket(models.Model):
user = models.ForeignKey(User)
event = models.ForeignKey(Event)
.....

Then I have a user who has 2 tickets for the same event. If I have the
event object and want to see who has tickets I could do this:

an_event.ticket_set.all()

However that will result in a list of tickets which isn't necessarily
a unique list of people who have tickets to the event. In my example
the user has 2 tickets for one event so I would get two ticket objects
back in my query set. How do I limit the query set results to say
don't give me more than one ticket for the event per user.

Hope that makes sense?

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

Offline

#2 Dec. 1, 2010 15:15:20

Tom E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Unique querysets


On Wed, Dec 1, 2010 at 3:07 PM, cootetom <coote...@gmail.com> wrote:
> Hi, I have a question about the ORM.
>
> If I have model class's:
>
> class Event(models.Model):
>    ....
>
> class Ticket(models.Model):
>    user = models.ForeignKey(User)
>    event = models.ForeignKey(Event)
>    .....
>
> Then I have a user who has 2 tickets for the same event. If I have the
> event object and want to see who has tickets I could do this:
>
> an_event.ticket_set.all()
>
> However that will result in a list of tickets which isn't necessarily
> a unique list of people who have tickets to the event. In my example
> the user has 2 tickets for one event so I would get two ticket objects
> back in my query set. How do I limit the query set results to say
> don't give me more than one ticket for the event per user.
>
> Hope that makes sense?
>

User.objects.filter(ticket__event=ev).distinct() ?

HTH

Tom

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

Offline

#3 Dec. 1, 2010 15:28:58

c.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Unique querysets


Excellent, thank you. That does the trick!




On Dec 1, 3:15 pm, Tom Evans <tevans...@googlemail.com> wrote:
> On Wed, Dec 1, 2010 at 3:07 PM, cootetom <coote...@gmail.com> wrote:
> > Hi, I have a question about the ORM.
>
> > If I have model class's:
>
> > class Event(models.Model):
> >    ....
>
> > class Ticket(models.Model):
> >    user = models.ForeignKey(User)
> >    event = models.ForeignKey(Event)
> >    .....
>
> > Then I have a user who has 2 tickets for the same event. If I have the
> > event object and want to see who has tickets I could do this:
>
> > an_event.ticket_set.all()
>
> > However that will result in a list of tickets which isn't necessarily
> > a unique list of people who have tickets to the event. In my example
> > the user has 2 tickets for one event so I would get two ticket objects
> > back in my query set. How do I limit the query set results to say
> > don't give me more than one ticket for the event per user.
>
> > Hope that makes sense?
>
> User.objects.filter(ticket__event=ev).distinct() ?
>
> HTH
>
> Tom

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

Offline

Board footer

Moderator control

Enjoy the 17th of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support