简体   繁体   中英

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?

You can use the F function as you did once:

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

Try:

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

This example might help.

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"}
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM