Search code examples
pythondjangodjango-ormdjango-annotate

How to use filter value as variable in django orm


I want to use tmp_id as a value of row id in extra method .

code:

order_obj = table.objects.filter()
.annotate(
     tmp_id=F('table2__test_data')
)
.extra(
      select={"val":"select id from data where row_id = {{here i want to use 
      tmp_id}} limit 1"}
)

can anyone tell me how to do it ?


Solution

  • I could not find any solution where we can use temp_id directly but there is an alternative where i can directly specify table2.test_data in extra method given below .

    order_obj = table.objects.filter()
    .annotate(
         tmp_id=F('table2__test_data')
    )
    .extra(
          select={"val":"select id from data where row_id = table2.test_data limit 1"}
    )