[Django]-Django ForeignKey filter issue

3👍

Yes. You can just use the key__ syntax like this:

products = Product.objects.filter(category__name='Tops')

Just make sure what is after the __ appears in the Category model and you will be good to go. You can read more about how django handles cross-relationship filtering here.


You can also just query by the related ID itself:

category = Category.objects.get(id=25)
products = Producct.objects.filter(category_id=25)
👤2ps

Leave a comment