Reference Signals Model signals
WARNING: For performance reasons, you shouldn’t perform queries in receivers of > pre_init or post_init signals because they would be executed for > each instance returned during queryset iteration.
766 sections in Django dev Search all versions →
Reference Signals Model signals
WARNING: For performance reasons, you shouldn’t perform queries in receivers of > pre_init or post_init signals because they would be executed for > each instance returned during queryset iteration.
Reference TemplateResponse and SimpleTemplateResponse
It can also be used as an alternative to calling render(). For example, the following view returns a TemplateResponse with a template and a context containing a queryset:
Reference Built-in template tags and filters Built-in tag referenceifBoolean operators
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 Generic views of objects
Well, if you’re dealing with a model object, this is already done for you. When you are dealing with an object or queryset, Django is able to populate the context using the lowercased version of the model class’ name.
Topic guide Built-in class-based generic views Generic views of objects
Often you need to present some extra information beyond that provided by the generic view. For example, think of showing a list of all the books on each publisher detail page.
Topic guide Built-in class-based generic views Generic views of objects
… 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 …
Topic guide Built-in class-based generic views Generic views of objects
Handily, the ListView has a get_queryset() method we can override. By default, it returns the value of the queryset attribute, but we can use it to add more logic.
Topic guide Built-in class-based generic views Generic views of objects
… 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.
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 Subclassing generic 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 Using mixins with class-based views
We’ll consider DetailView, which renders a “detail” view of an object, and ListView, which will render a list of objects, typically from a queryset, and optionally paginate them.
Topic guide Using mixins with class-based views Building up Django’s generic class-based views
… as declared in the URLConf, and looks the object up either from the model attribute on the view, or the queryset attribute if that’s provided). SingleObjectMixin also overrides get_context_data(), which is used across all Django’s built in class-based views to …
Topic guide Using mixins with class-based views Using Django’s class-based view mixins
One way to do this is to combine ListView with SingleObjectMixin, so that the queryset for the paginated list of books can hang off the publisher found as the single object.
Topic guide Aggregation Joins and aggregates
… (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 …
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 …
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 Custom 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 …
Topic guide Managers Custom 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 Custom managersBase 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.
Topic guide Managers Custom managersBase managers
Therefore, you should not override get_queryset() to filter out any rows. If you do so, Django will return incomplete results.