Django queryset filter for backwards related fields
Try this. I haven't tested it let me know if you have any issues
#Untested Code
Project.objects.filter(action__person = person)
Is it true that you have a many-to-many relation between Person
and Project
? If so, you can simplify your setup like this:
class Person(models.Model):
projects = models.ManyToManyField('Project')
name = models.CharField(max_length=100) # just an example
class Project(models.Model):
# ... some fields here ...
You can then e.g. issue the following query to get all the projects from people who are called John:
Project.objects.filter(person_set__name="John")
The usage of select_related()
can speed up the lookup a bit when you have lots of queries which follow relations between different database tables but you don't need it to accomplish what you want.