Ad

How Add Multiple Fliters To A Model

- 1 answer

I have inventory, status and transaction tables. Transaction has many to one inventory link and inventroy has one to one relation to status.

I am trying to pull all the transaction with check out > 3 sep and inventory which has staus checkout in Django.

I am using following syntax to pull in pyton, it give error

   inventory = Inventory.objects.get(status = 2)
   transactions1 = Transaction.objects.filter((checkout_time__gt='2019-09-03') and inventory__in=inventory.id)
Ad

Answer

You can simply chain filters if you're looking for an AND statemet like this:

Transaction.objects.filter(checkout_time__gt='2019-09-03', inventory__status=2)

If you want to be more explicit you can use Django's Q object:

from django.models import Q
Transaction.objects.filter(Q(checkout_Time__gt='2019-09-03'), Q(inventory__status=2))

Using django's Q object you can build OR queries with | operator like this:

Transaction.objects.filter(Q(checkout_Time__gt='2019-09-03') | Q(inventory__status=2))
Ad
source: stackoverflow.com
Ad