JacobF
JacobF

Reputation: 2485

Django get all records of related models

I have 3 models for a to-do list app:

class Topic(models.model)
    user = models.ForeignKey(UserProfile)
    lists = models.ManyToManyField(List)

class List(models.model)
    activities = models.ManyToManyField(Activity)

class Activity(models.model)
    activity = models.CharField(max_length=250)

This makes sense when a user selects a Topic, then a List (sub-category), which shows all activities on that list.

But how would I efficiently query things like

Would I need to use select_related() in the query and than loop trough the related objects, or is there a more efficient way without looping? (Or should I change my models?)

Upvotes: 10

Views: 18028

Answers (2)

Daniel Roseman
Daniel Roseman

Reputation: 599540

Use the double-underscore syntax to query across relationships.

All activities for user:

Activity.objects.filter(list__topic__user=my_user)

All activities for user for a topic:

Activity.objects.filter(list__topic=my_topic)

(Note that currently a Topic is for a single user only. Not sure if that's what you mean: you describe a user selecting a topic, which couldn't happen here. Potentially the link from Topic to UserProfile should go the other way, or be a ManyToMany.)

Upvotes: 12

McAbra
McAbra

Reputation: 2514

Give them related names (it's easier to manage):

class Topic(models.model)
    user = models.ForeignKey(UserProfile, related_name = 'topics')
    lists = models.ManyToManyField(List, related_name = 'topics')

class List(models.model)
    activities = models.ManyToManyField(Activity, related_name = 'lists')

class Activity(models.model)
    activity = models.CharField(max_length=250)

Then you can do awesome things:

user = UserProfile.objects.get(pk=1) # for example
user.topics.all() # returns all topics

topic = Topic.objects.get(pk=1) # for example
topic.users.get(pk=1) # returns user
lists = topic.lists.all() # returns List object instances QuerySet

for list in lists:
    list.activites.all()

Handy info: https://docs.djangoproject.com/en/dev/ref/models/relations/

Upvotes: 1

Related Questions