Reputation: 153
Like the title says I need to rename some items in a queryset. The code for the queryset is made like this:
data = SnpsFunctionalelement.objects.values('snpid__rsid', 'elementid__name', 'celllineid__name', 'countexperiments', 'filetype')
i tried to rename the values using from django.db.models import F
but if I do
data = SnpsFunctionalelement.objects.values(rsid=F('snpid__rsid'), FunctionalElement=F('elementid__name'), CellLine=F('celllineid__name'),
countexperiments=F('countexperiments'), filetype=F('filetype'))
it gives me an error because countexperiments
and filetype
are already the names assigned to the model to which they belong.
So is there a way to rename only a part of the values of the queryset mantaining the remaining ones?
Upvotes: 1
Views: 1614
Reputation: 477190
The .values(..)
method [Djang-doc] accepts both position parameters (strings of the names of the fields), and named parameters (expressions with alias names). For example:
SnpsFunctionalelement.objects.values(
'countexperiments',
'filetype',
rsid=F('snpid__rsid'),
FunctionalElement=F('elementid__name'),
CellLine=F('celllineid__name')
)
Note that, as always in Python, the positional arguments should be placed before the named parameters (since otherwise the "position" is a rather vague concept).
Upvotes: 4