[Fixed]-Django QuerySet class attributes

1đź‘Ť

âś…

It’s not nice that it appears to be necessary, but there is a “source” link next to the line that says what the class definition looks like. That leads to its __init__ method:

def __init__(self, model=None, query=None, using=None, hints=None):
    self.model = model
    self._db = using
    self._hints = hints or {}
    self.query = query or sql.Query(self.model)
    self._result_cache = None
    self._sticky_filter = False
    self._for_write = False
    self._prefetch_related_lookups = []
    self._prefetch_done = False
    self._known_related_objects = {}  # {rel_field, {pk: rel_obj}}
    self._iterable_class = ModelIterable
    self._fields = None

From this we can see that self.model and self.query exist, but all the others are private and you shouldn’t use them.

From scrolling further, there’s also the property, queryset.db, the database that would be used if the query were to run now.

👤RemcoGerlich

Leave a comment