Django Many-To-One relationship filter set

kevin.w.johnson

I have a couple models setup like so:

Group(models.Model):
    name = models.TextField(max_length=255)

Thing(models.Model):
    location = models.TextField(max_length=255)
    group = models.ForeignKey(Group)

That's just an example to illustrate the relationship, so forgive any syntax errors.

My question is, how can find a group which has a particular set of locations? I should be able to access the Things associated with a Group by using:

Group.thing_set

Right? So is there any way I can filter based on what items are within the thing_set? I'm thinking something along the lines of this.

Group.objects.filter(thing_set.location in ["Location A", "Location B"]).all()

Hopefully this would return to me every group which contains things from both Location A and Location B. Any suggestions or a push in the right direction would be very helpful!

Thanks.

Sebastian Wozny

According to the documentation you have to use the name of the model as your query operator:

To refer to a “reverse” relationship, just use the lowercase name of the model.

models.py:

from django.db import models


class Group(models.Model):
   name = models.TextField(max_length=255)


class Thing(models.Model):
    location = models.TextField(max_length=255)
    group = models.ForeignKey(Group)

views.py:

from django.views.generic import ListView


class ReverseFK(ListView):
    model = Group

    def get_queryset(self):
        g = Group.objects.create(name="example")
        g.save()
        Thing.objects.create(location="here", group=g)
        return Group.objects.filter(thing__location__in=["here","there"])

Working code example on github

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

django - Get the set of objects from Many To One relationship

From Dev

Django 1.11 One-to-Many relationship no related set

From Dev

How to filter in many-to-many relationship in Django

From Dev

Django query in One to Many relationship

From Dev

Django one to many Relationship Query

From Dev

SQLAlchemy one to many relationship, how to filter the collection

From Dev

SQLAlchemy one to many relationship, how to filter the collection

From Dev

Django Many to Many Query Set Filter

From Dev

Django one to many relationship: number of objects

From Dev

Django Form with a one-to-many relationship

From Dev

Better Django UI for one to many relationship

From Dev

Django displaying one to many relationship in the admin page

From Dev

Serializing One to Many Relationship in django REST framework

From Dev

Django, Many-to-one relationship with Abstract entities

From Dev

Django Form with a one-to-many relationship

From Dev

Django one to many relationship: number of objects

From Dev

creating a many to one relationship in django with an existing model

From Dev

Django models in Many2Many relationship: filter by a group of member

From Dev

Django query set filter over relationship with concat

From Dev

Django many to many relationship

From Dev

How to use a subquery to filter a sqlalchemy query on a one to many relationship?

From Dev

CoreData: Using NSPredicate to filter one to many to many relationship (error to-many key not allowed here)

From Dev

One-to-many relationship: how to set default entity-value?

From Dev

SQLite One-to-Many Relationship Set/Get Foreign Key

From Dev

One-to-many relationship: how to set default entity-value?

From Dev

Django: Query one to many relationship for existing relationship with certain attributes in the many-model

From Dev

Django Rest Framework 3.0: Saving Nested, Many-To-One Relationship

From Dev

Django many-to-one relationship: NOT NULL constraint failed

From Dev

Django Postgres ArrayField vs One-to-Many relationship

Related Related

  1. 1

    django - Get the set of objects from Many To One relationship

  2. 2

    Django 1.11 One-to-Many relationship no related set

  3. 3

    How to filter in many-to-many relationship in Django

  4. 4

    Django query in One to Many relationship

  5. 5

    Django one to many Relationship Query

  6. 6

    SQLAlchemy one to many relationship, how to filter the collection

  7. 7

    SQLAlchemy one to many relationship, how to filter the collection

  8. 8

    Django Many to Many Query Set Filter

  9. 9

    Django one to many relationship: number of objects

  10. 10

    Django Form with a one-to-many relationship

  11. 11

    Better Django UI for one to many relationship

  12. 12

    Django displaying one to many relationship in the admin page

  13. 13

    Serializing One to Many Relationship in django REST framework

  14. 14

    Django, Many-to-one relationship with Abstract entities

  15. 15

    Django Form with a one-to-many relationship

  16. 16

    Django one to many relationship: number of objects

  17. 17

    creating a many to one relationship in django with an existing model

  18. 18

    Django models in Many2Many relationship: filter by a group of member

  19. 19

    Django query set filter over relationship with concat

  20. 20

    Django many to many relationship

  21. 21

    How to use a subquery to filter a sqlalchemy query on a one to many relationship?

  22. 22

    CoreData: Using NSPredicate to filter one to many to many relationship (error to-many key not allowed here)

  23. 23

    One-to-many relationship: how to set default entity-value?

  24. 24

    SQLite One-to-Many Relationship Set/Get Foreign Key

  25. 25

    One-to-many relationship: how to set default entity-value?

  26. 26

    Django: Query one to many relationship for existing relationship with certain attributes in the many-model

  27. 27

    Django Rest Framework 3.0: Saving Nested, Many-To-One Relationship

  28. 28

    Django many-to-one relationship: NOT NULL constraint failed

  29. 29

    Django Postgres ArrayField vs One-to-Many relationship

HotTag

Archive