简体   繁体   中英

Django queryset get all fields in values() plus a foreign key field

I have a model with some fields, one of them is a FK. I would like to use

MyModel.objects.all().defer("pk").values()

and to add a field of my FK (myfk__name) without writing all model fields + 1 in the values.

I also would like to avoid doing another query after this one to add it manually.

Is it possible?

使用annotate

MyModel.objects.all().defer('pk').values().annotate(name=F('myfk__name'))

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