db.models.query.QuerySet.db

db The database that will be used if this query is executed now. Note The query parameter to QuerySet exists so that specialized query subclasses such as GeoQuerySet can reconstruct internal query state. The value of the parameter is an opaque representation of that query state and is not part of a public API. To put it simply: if you need to ask, you don’t need to use it.

db.models.query.QuerySet.defer()

defer(*fields) In some complex data-modeling situations, your models might contain a lot of fields, some of which could contain a lot of data (for example, text fields), or require expensive processing to convert them to Python objects. If you are using the results of a queryset in some situation where you don’t know if you need those particular fields when you initially fetch the data, you can tell Django not to retrieve them from the database. This is done by passing the names of the field

db.models.query.QuerySet.create()

create(**kwargs) A convenience method for creating an object and saving it all in one step. Thus: p = Person.objects.create(first_name="Bruce", last_name="Springsteen") and: p = Person(first_name="Bruce", last_name="Springsteen") p.save(force_insert=True) are equivalent. The force_insert parameter is documented elsewhere, but all it means is that a new object will always be created. Normally you won’t need to worry about this. However, if your model contains a manual primary key value that

db.models.query.QuerySet.dates()

dates(field, kind, order='ASC') Returns a QuerySet that evaluates to a list of datetime.date objects representing all available dates of a particular kind within the contents of the QuerySet. field should be the name of a DateField of your model. kind should be either "year", "month" or "day". Each datetime.date object in the result list is “truncated” to the given type. "year" returns a list of all distinct year values for the field. "month" returns a list of all distinct year/month value

db.models.query.QuerySet.bulk_create()

bulk_create(objs, batch_size=None) This method inserts the provided list of objects into the database in an efficient manner (generally only 1 query, no matter how many objects there are): >>> Entry.objects.bulk_create([ ... Entry(headline="Django 1.0 Released"), ... Entry(headline="Django 1.1 Announced"), ... Entry(headline="Breaking: Django is awesome") ... ]) This has a number of caveats though: The model’s save() method will not be called, and the pre_save and post_

db.models.query.QuerySet.count()

count() Returns an integer representing the number of objects in the database matching the QuerySet. The count() method never raises exceptions. Example: # Returns the total number of entries in the database. Entry.objects.count() # Returns the number of entries whose headline contains 'Lennon' Entry.objects.filter(headline__contains='Lennon').count() A count() call performs a SELECT COUNT(*) behind the scenes, so you should always use count() rather than loading all of the record into Pyt

db.models.query.QuerySet.aggregate()

aggregate(*args, **kwargs) Returns a dictionary of aggregate values (averages, sums, etc.) calculated over the QuerySet. Each argument to aggregate() specifies a value that will be included in the dictionary that is returned. The aggregation functions that are provided by Django are described in Aggregation Functions below. Since aggregates are also query expressions, you may combine aggregates with other aggregates or values to create complex aggregates. Aggregates specified using keyword a

db.models.query.QuerySet.all()

all() Returns a copy of the current QuerySet (or QuerySet subclass). This can be useful in situations where you might want to pass in either a model manager or a QuerySet and do further filtering on the result. After calling all() on either object, you’ll definitely have a QuerySet to work with. When a QuerySet is evaluated, it typically caches its results. If the data in the database might have changed since a QuerySet was evaluated, you can get updated results for the same query by calling

db.models.query.QuerySet.annotate()

annotate(*args, **kwargs) Annotates each object in the QuerySet with the provided list of query expressions. An expression may be a simple value, a reference to a field on the model (or any related models), or an aggregate expression (averages, sums, etc.) that has been computed over the objects that are related to the objects in the QuerySet. Each argument to annotate() is an annotation that will be added to each object in the QuerySet that is returned. The aggregation functions that are pr

db.models.prefetch_related_objects()

prefetch_related_objects(model_instances, *related_lookups) [source] New in Django 1.10. Prefetches the given lookups on an iterable of model instances. This is useful in code that receives a list of model instances as opposed to a QuerySet; for example, when fetching models from a cache or instantiating them manually. Pass an iterable of model instances (must all be of the same class) and the lookups or Prefetch objects you want to prefetch for. For example: >>> from django.db.mo