Often in views, one can find code that looks like:
from django.shortcuts import render
def some_view(request):
my_objects = MyModel.objects.values()
return render(request, 'some_template.html', {'my_objects': my_objects})
The .values(…)
[Django-doc] part will return a (QuerySet
) of dictionaries, not MyModel
objects.
Dictionaries are less "rich". These simply map keys to values. A model enhances that with several extra functionalities:
get_fieldname_display
ForeignKey
s act like lazy queries); andThese are typical problems that arise by the primitive obsession antipattern [refactoring.guru].
Do not make use of .values(…)
unless in certain circumstances. One can make use of .values(…)
for example to group by a certain value. But normally using .values()
is not a good idea, one thus better creates a query that looks like:
from django.shortcuts import render
def some_view(request):
my_objects = MyModel.objects.all()
return render(request, 'some_template.html', {'my_objects': my_objects})
Sometimes people make use of .values(…)
to boost queries, by only selecting columns they are interested in. One can make use of .only(…)
[Django-doc] and .defer(…)
[Django-doc] to retrieve only a subset of the columns of the model. The remaining columns are then lazy loaded with extra queries when necessary.