'Django model default value in response
I want to have a default value in my django model response value
Sample model query
myModel.objects.filter().values("username", "user_gender")
I want to have a default value in response
It must be like
Select username, user_gender, 'delhi' as country from mytable
Please let me know if any suggestions
Solution 1:[1]
You can add additional value to queryset using Value
expression:
from django.db.models import CharField, Value
myModel.objects.filter().values("username", "user_gender", default_city=Value('delhi', output_field=CharField()))
You can find more details about values()
method here.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |