2π
β
In the view, you can do:
def single_animal(request,id):
animal = AniamalFile.objects.get(id=id)
animal_category=None
categories = Category.objects.filter(food__main=animal)
if categories:
animal_category = categories[0]
You can pass this as a context variable, and access it as {{animal_category}}
Or if you want to show all the categories, just send categories
in the context and in the template:
{% for cat in categories %}{{cat.name}} {% endfor %}
Alternatively,
def single_animal(request,id):
animal = AniamalFile.objects.get(id=id)
animal_category=None
foodset = animal.food_set.all()
categories = Category.objects.filter(food__in=foodset)
π€karthikr
Source:stackexchange.com