Deomachus
Deomachus

Reputation: 179

Django Query - Retrieving associated foreign keys

This is a very simple question, and I apologize if it's a duplicate, but I can't seem to find an answer anywhere. I have the following model:

class CustomForm(models.Model):

  ...
  form = models.OneToOneField("forms.EvalType")
  author = models.ForeignKey("accounts.User", related_name='+')
  ...

Each author may be associated with several CustomForms. I need to retrieve all form's ("EvalType's") associated with a particular author (via the CustomForm model). How would I write that statement for Django?

Upvotes: 0

Views: 50

Answers (1)

Daniel Roseman
Daniel Roseman

Reputation: 599490

Always start from the model type you want to end up with. Then you can use the double-underscore syntax to traverse relationships.

EvalType.objects.filter(customform__author=my_author)

Upvotes: 3

Related Questions