Benjamin
Benjamin

Reputation: 3467

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?

Upvotes: 2

Views: 2531

Answers (1)

dirkgroten
dirkgroten

Reputation: 20672

use annotate:

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

Upvotes: 3

Related Questions