Reputation: 673
maybe it is not good practice but I would like to know if it is possible to decode an integer field value with its string value I know I should have a specific models linked as thesaurus...
I have a models mymodel I produce a QuerySet mymodel.objects.values('id','var2','var3','var4') for example var3 is an integer (0/1) for Yes/No answer
Is it possible to populate my QuerySet with Yes or No instead of its integer value?
Upvotes: 1
Views: 946
Reputation: 476584
You can annotate it, for example:
from django.db.models import Case, CharField, Value, When
QuerySet.objects.values('id', 'var2', 'var4',
new_var3=Case(
When(var3=1, then=Value('Yes')),
default=Value('No'),
output_field=CharField(),
)
)
Note that you however should rename the variable.
That being said, it is not a good idea to use .values(..)
or .values_list(..)
to eprform serialization. Furthermore it will perform text processing on the database, and often that is not really the core task of a database. Normally serializers [drf-doc] are used for that.
Upvotes: 1