Multi-table lookup ... ?

4 views
Skip to first unread message

dbee

unread,
Sep 8, 2007, 8:14:52 AM9/8/07
to Django users
# People who need to be reminded
class Reminders (models.Model):
userProfile = models.ForeignKey(UserProfile)
groups = models.ManyToManyField(Groups, blank=True, null=True)
campaigns = models.ManyToManyField(Campaigns, blank=True,
null=True)

# Campaign that needs to be sent out to People
class Campaigns (models.Model):
userProfile = models.ForeignKey(UserProfile)
groups = models.ManyToManyField(Groups, blank=True, null=True)

# Groups of People
class Groups (models.Model):
userProfile = models.ForeignKey(UserProfile)
name = models.CharField( maxlength=100 )
groups_hash = models.CharField( maxlength=16 )
description = models.CharField( maxlength=100 )

If I have a campaign object that has 3 groups in it. And I want to
find all the reminder objects that are in those groups ... unless of
course it's a campaign with no groups in it - in which case I'd like
all the reminders associated with that campaign ?

This is my attempt, the second reminders statement won't work. I'm
wondering what the best way to go about a query like this would be ?

# campaign object from db
campaign = <campaign_object>

groups_list = campaign.groups.all()

# Are there groups ?
if groups_list.count() > 0 :

# find the reminders ??????
reminders = campaign.groups.reminders_set.all()

else:

reminders = Reminders.objects.get(campaigns=campaign)

Message has been deleted

James Bennett

unread,
Sep 8, 2007, 8:31:21 AM9/8/07
to django...@googlegroups.com
On 9/8/07, dbee <darab...@gmail.com> wrote:
> If I have a campaign object that has 3 groups in it. And I want to
> find all the reminder objects that are in those groups ... unless of
> course it's a campaign with no groups in it - in which case I'd like
> all the reminders associated with that campaign ?

if campaign.group_set.count():
reminders =
Reminders.objects.filter(campaigns__groups__id__in=[g.id for g in
campaign.group_set.all())
else:
reminders = Reminders.objects.filter(campains__pk=campaign.id)


--
"Bureaucrat Conrad, you are technically correct -- the best kind of correct."

Chris Hoeppner

unread,
Sep 8, 2007, 8:31:41 AM9/8/07
to django...@googlegroups.com
I was just wondering. Consider this an "aside" or of the kind.

Why hasn't anyone thought of something like capistrano for django. (Yeah
I know it can work, but there're a few features for RoR'ers).

James Bennett

unread,
Sep 8, 2007, 8:32:51 AM9/8/07
to django...@googlegroups.com
On 9/8/07, Chris Hoeppner <hoep...@gmail.com> wrote:
> Why hasn't anyone thought of something like capistrano for django. (Yeah
> I know it can work, but there're a few features for RoR'ers).

Lots of people have thought of it. What they haven't done is written
it. Want to be the first? ;)

Chris Hoeppner

unread,
Sep 8, 2007, 8:37:39 AM9/8/07
to django...@googlegroups.com
Sure. Anyone to join me?

Jon Atkinson

unread,
Sep 9, 2007, 3:56:29 PM9/9/07
to django...@googlegroups.com
Sure :-)

Andrey Khavryuchenko

unread,
Sep 9, 2007, 5:32:27 PM9/9/07
to django...@googlegroups.com
Chris,

CH> Sure. Anyone to join me?

Show me a code and I'll join, since have a nagging need for such tool.

--
Andrey V Khavryuchenko
Django NewGate - http://www.kds.com.ua/djiggit/
Development - http://www.kds.com.ua
Call akhavr1975 on www.gizmoproject.com

qwerty

unread,
Sep 9, 2007, 6:19:32 PM9/9/07
to django...@googlegroups.com
I know this isn't capistriano but there is a work already in pythonpaste for django: http://pythonpaste.org/djangopaste/

Maybe instead of work in something new that can be fixed/finished =)

2007/9/9, Andrey Khavryuchenko <akh...@gmail.com>:

dbee

unread,
Sep 10, 2007, 5:10:29 PM9/10/07
to Django users
Hmm, I'm not entirely sure where you got the campaign.groups_set
construction there James. There is no campaign.groups_set afaik ...

I've been trying to mess about with it and what I've gotten so far is
this ...

# Get the details of the reminders to whom this campaign
will be
sent
if campaign.groups.all() > 0 :

# Each campaign has groups and each group has reminder
profiles
reminders_list =


Reminders.objects.filter(campaigns__groups__id__in=[g.id for g in

campaign.groups.all()])

else:

# Campaigns have no groups - send to
all
reminders_list =
Reminders.objects.filter(campaigns__pk=campaign.id)

The first time I try to access reminders_list for the 'if' clause ...
i get a programming error ...

thanks btw


On Sep 8, 1:31 pm, "James Bennett" <ubernost...@gmail.com> wrote:

Chris Brand

unread,
Sep 10, 2007, 5:18:07 PM9/10/07
to django...@googlegroups.com
dbee wrote:

>Hmm, I'm not entirely sure where you got the campaign.groups_set
>construction there James. There is no campaign.groups_set afaik ...
>
>
>

Probably from here :
http://djangoproject.com/documentation/db-api/#related-objects

Did you try it or just assume that James made a mistake ?

Chris

dbee

unread,
Sep 10, 2007, 7:09:33 PM9/10/07
to Django users
As far as I understand it, campaign.groups_set would only be relevant
for a 'backwards' relationship. Whereas in this case campaign.group is
actually relevant.

Yes, I tried it - and I tried many other combinations as well ...

Thanks

Reply all
Reply to author
Forward
0 new messages