[Django]-Get all instances from related models

3👍

Use a reverse query by using the model name.

Get a ShoesItem:

shoes_item = ShoesItem.objects.all()[0]

Get ShoesSize objects for the ShoesItem via Shoes object:

sizes = ShoesSize.objects.filter(shoes__name=shoes_item)

Get Store objects for the ShoesItem via Shoes object:

shops = Store.objects.filter(shoes__name=shoes_item)

See more – Lookups that span relationships

For a queryset of ShoesItem:

shoes_items = ShoesItem.objects.filter(has_shop=True)
sizes = ShoesSize.objects.filter(shoes__name__in=shoes_items)
shops = Store.objects.filter(shoes__name__in=shoes_items)
👤sunn0

Leave a comment