84
Itβs a bit hacky, but you could use the extra
method:
MyModel.objects.extra(
select={
'renamed_value': 'cryptic_value_name'
}
).values(
'renamed_value'
)
This basically does SELECT cryptic_value_name AS renamed_value
in the SQL.
Another option, if you always want the renamed version but the db has the cryptic name, is to name your field with the new name but use db_column
to refer to the original name in the db.
282
From django>=1.8
you can use annotate and F object
from django.db.models import F
MyModel.objects.annotate(renamed_value=F('cryptic_value_name')).values('renamed_value')
Also extra()
is going to be deprecated, from the django docs:
This is an old API that we aim to deprecate at some point in the future. Use it only if you cannot express your query using other queryset methods. If you do need to use it, please file a ticket using the QuerySet.extra keyword with your use case (please check the list of existing tickets first) so that we can enhance the QuerySet API to allow removing extra(). We are no longer improving or fixing bugs for this method.
- [Django]-What's the purpose of Django setting βSECRET_KEYβ?
- [Django]-Django Installed Apps Location
- [Django]-South migration: "database backend does not accept 0 as a value for AutoField" (mysql)
160
Without using any other manager method (tested on v3.0.4
):
from django.db.models import F
MyModel.objects.values(renamed_value=F('cryptic_value_name'))
Excerpt from Django docs:
An F() object represents the value of a model field or annotated
column. It makes it possible to refer to model field values and
perform database operations using them without actually having to pull
them out of the database into Python memory.
- [Django]-How do I use an UpdateView to update a Django Model?
- [Django]-Django models: Only permit one entry in a model?
- [Django]-How do you serialize a model instance in Django?
1
I am working with django 1.11.6
( And the key:value pair is opposite to that of accepted answer )
This is how i am making it work for my project
def json(university):
address = UniversityAddress.objects.filter(university=university)
address = address.extra(select={'city__state__country__name': 'country', 'city__state__name': 'state', 'city__name': 'city'})
address = address.values('country', 'state', "city", 'street', "postal_code").get()
return address
Note that adding simultanous objects.filter().extra().values() is same as above.
- [Django]-Annotate with latest related object in Django
- [Django]-Django Reverse with arguments '()' and keyword arguments '{}' not found
- [Django]-Paginate relationship in Django REST Framework?
0
Try passing as kwargs:
MyModel.objects.annotate(**{'A B C':F('profile_id')}).values('A B C')
In my case, there were spaces and other special characters included in the key of each value in the result set so this did the trick.
- [Django]-Django: Arbitrary number of unnamed urls.py parameters
- [Django]-Django β what is the difference between render(), render_to_response() and direct_to_template()?
- [Django]-Django auto_now and auto_now_add
-6
Its more than simple if you want to rename few fields of the mode.
Try
projects = Project.objects.filter()
projects = [{'id': p.id, 'name': '%s (ID:%s)' % (p.department, p.id)} for p in projects]
Here i do not have a name
field in the table, but i can get that after tweaking a little bit.
- [Django]-How to select a record and update it, with a single queryset in Django?
- [Django]-How to use refresh token to obtain new access token on django-oauth-toolkit?
- [Django]-Django: Reverse for 'detail' with arguments '('',)' and keyword arguments '{}' not found