Reference
Model field reference
Field types
classDateField(auto_now=False,auto_now_add=False,**options)[source] A date, represented in Python by a datetime.date instance. Has a few extra, optional arguments: DateField.auto_now : Automatically set the field to now every time the object is saved. Useful for “last-modified” timestamps.
Reference
Model field reference
classField[source] : Field is an abstract class that represents a database table column. Django uses fields to create the database table (db_type()), to map Python types to database (get_prep_value()) and vice-versa (from_db_value()).
Reference
Model field reference Field attribute reference Model index reference Constraints reference Model _meta API Related objects reference Model class reference Model Meta options Model instance reference QuerySet API reference Lookup API reference Query Expressions Conditional Expressions Database Functions
Reference
This document describes the details of the Model API. It builds on the material presented in the model and database query guides, so you’ll probably want to read and understand those documents before reading this one.
Reference
Model instance reference
Saving objects
If save() is passed a list of field names in keyword argument update_fields, only the fields named in that list will be updated.
Reference
Model instance reference
Otherwise, for database variants such as DB_CASCADE, the return value will report only instances of the QuerySet’s model. For more details, including how to delete objects in bulk, see Deleting objects.
dev
Reference
Model instance reference
Other attributes
Model._state : The _state attribute refers to a ModelState object that tracks the lifecycle of the model instance.
Reference
Lookup API reference
classLookup[source] : A Lookup is a generic class to implement lookups.
Reference
Model Meta options
Available Meta options
Options.get_latest_by : The name of a field or a list of field names in the model, typically DateField, DateTimeField, or IntegerField. This specifies the default field(s) to use in your model Manager’s latest() and earliest() methods.
Reference
classRelatedManager : A “related manager” is a manager used in a one-to-many or many-to-many related context. This happens in two cases:
Reference
Paginator
Page class
Paginator.ELLIPSIS : A translatable string used as a substitute for elided page numbers in the page range returned by get_elided_page_range(). Default is '…'. Paginator.count[source] : The total number of objects, across all pages. Paginator.num_pages[source] : The total number of pages.
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.