djangodocs.org

769 sections in Django dev Search all versions →

in operator

Reference Built-in template tags and filters

Contained within. This operator is supported by many Python containers to test whether the given value is in the container. The following are some examples of how x in y will be interpreted:

Topic guide Built-in class-based generic views

… specify the objects that the view will operate upon – you can also specify the list of objects using the queryset argument: Specifying model = Publisher is shorthand for saying queryset = Publisher.objects.all(). However, by using queryset to define a …

Performing extra work

Topic guide Built-in class-based generic views

… is the default > name that DetailView uses to find the value of the primary key used to > filter the queryset. If you want to call the group something else, you can set > pk_url_kwarg > on the view.

Model forms

Topic guide Form handling with class-based views

If a queryset is given, the model for that queryset will be used. Model form views provide a form_valid() implementation that saves the model automatically. You can override this if you have any special requirements; see below for examples.

Topic guide Class-based views

Suppose somebody wants to access our book library over HTTP using the views as an API. The API client would connect every now and then and download book data for the books published since last visit.

Topic guide Aggregation

… most aggregates, there is no way to avoid this problem, however, the Count aggregate has a distinct parameter that may help: NOTE: In order to understand what happens in your query, consider inspecting the > query property of your QuerySet.

Topic guide Aggregation

… (note how we use 'book' to specify the Publisher -> Book reverse foreign key hop): (Every Publisher in the resulting QuerySet will have an extra attribute called book__count.) We can also ask for the oldest book of any of those …

Filtering on annotations

Topic guide Aggregation

It’s more efficient to use QuerySet.filter() to exclude > rows. The aggregation filter argument is only useful when using two or > more aggregations over the same relations with different conditionals.

Fetch modes

Topic guide

… can customize the behavior of this fetching with a fetch mode, making it more efficient or even blocking it. Use QuerySet.fetch_mode() to set the fetch mode for model instances fetched by a QuerySet: Fetch modes apply to: ForeignKey fields OneToOneField …

Available modes

Topic guide Fetch modes

We’ll explain them below using these models: …and this loop: …where books is a QuerySet of Book instances using some fetch mode. FETCH_ONE Fetches the missing field for the current instance only. This is the default mode.

Topic guide Managers

… Manager methods.) For example, this custom Manager adds a method with_counts(): With this example, you’d use OpinionPoll.objects.with_counts() to get a QuerySet of OpinionPoll objects with the extra num_responses attribute attached. A custom Manager method can return anything you want. It …

Default managers

Topic guide Managers

As a result, it’s a good idea to be careful in your choice of default manager in order to avoid a situation where overriding get_queryset() results in an inability to retrieve objects you’d like to work with.

Topic guide Managers

For example, if the Question model from the tutorial had a deleted field and a base manager that filters out instances with deleted=True, a queryset like Choice.objects.filter(question__name__startswith='What') would include choices related to deleted questions.