I am trying to understand the django rest framework generic API views, the documentation tells me that while overriding methods of the generic views, I shouldn't be accessing the queryset
attribute directly and instead access the get_queryset()
function because apparently the queryset attribute is evaluated only once, what does the get_queryset()
do differently?
Does the queryset attribute ever get updated?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Django __str__ returned non-string (type NoneType)
- Evil ctypes hack in python
To further expand, the source code will tell you the truth.
The warning about using self.queryset directly is that results are cached when evaluated so further references to this value will not be up to date as opposed of using self.get_queryset().
for simple views, the
queryset
attribute would quickly get your started. But in real life, normally, we have permissions on models, for example, the defaultqueryset
for a user would be filtered queryset based on request.user. Theget_queryset
method would becomethe above
get_queryset
method is just an example present real-life API design.