Milad Khodabandehloo
Milad Khodabandehloo

Reputation: 1977

Django annotate a static value to queryset

Is it possible to add an static field value to django query set by annotate or any other way? for example i have an query set called rooms. I want all rooms have a static field in query set not in database. for example:

rooms[0].some_field = "static_value"

Thanks in advance.

Upvotes: 7

Views: 5232

Answers (1)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477749

Yes, you can annotate this with Value [Django-doc]:

from django.db.models import CharField, Value

MyModel.objects.annotate(
    some_field=Value('static_value', output_field=CharField())
)

Upvotes: 17

Related Questions