0

Im new to Django. Anyone help me to write "Not In" sql query in django. here i used raw() query set without raw() query set how to write this query in django.

query = 'SELECT basic_uom FROM uom_master WHERE id="'+ id +'" and basic_uom not in (SELECT next_uom from uom_master WHERE id="'+ id +'") and basic_uom not in(SELECT std_uom FROM std_uom_master WHERE id"'+ id +'")ORDER BY next_uom ASC'
data = uom_master.objects.raw(query)
Satvik
  • 127
  • 1
  • 2
  • 10

1 Answers1

1

If [1,2,3] is your list then you can do something like this, you modify according to your table names

uom_master.objects.filter(id=some_id).exclude(id__in=[1,2,3]).order_by("next_uom")

To get list of ids you can do like

list_of_ids = std_uom_master.objects.filter().values_list("id",flat=True)

filter is your filtering criteria and exclude is records to be omitted.

See filters like id__in and guide about making queries

Yugandhar Chaudhari
  • 3,831
  • 3
  • 24
  • 40