Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

It must be like

Select username, user_gender, 'delhi' as country from mytable

Please let me know if any suggestions

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading