Reference
Model instance reference
If you delete a field from a model instance, accessing it again reloads the value from the database: Model.refresh_from_db(using=None,fields=None,from_queryset=None)[source] Model.arefresh_from_db(using=None,fields=None,from_queryset=None) Asynchronous version: arefresh_from_db() If you need to reload a model’s values from the database, you can use the refresh_from_db() method.
Reference
Signals
Model signals
django.db.models.signals.pre_delete Sent at the beginning of a model’s delete() method and a queryset’s delete() method. Arguments sent with this signal: sender The model class. instance The actual instance being deleted. using The database alias being used.
Reference
Signals
Model signals
django.db.models.signals.post_delete Like pre_delete, but sent at the end of a model’s delete() method and a queryset’s delete() method. Arguments sent with this signal: sender : The model class. instance : The actual instance being deleted.
Topic guide
Asynchronous support
Async views
With some exceptions, Django can run ORM queries asynchronously as well: Detailed notes can be found in Asynchronous queries, but in short: All QuerySet methods that cause an SQL query to occur have an a-prefixed asynchronous variant.
Topic guide
Using mixins with class-based views
Building up Django’s generic class-based views
Lists of objects follow roughly the same pattern: we need a (possibly paginated) list of objects, typically a QuerySet, and then we need to make a TemplateResponse with a suitable template using that list of objects.
Topic guide
Managers
There are two reasons you might want to customize a Manager: to add extra Manager methods, and/or to modify the initial QuerySet the Manager returns.
Topic guide
Database access optimization
Use QuerySet.explain() to understand how specific QuerySets are executed by your database. You may also want to use an external project like django-debug-toolbar, or a tool that monitors your database directly.
Topic guide
Database access optimization
For instance: At the most basic level, use filter and exclude to do filtering in the database. Use F expressions to filter based on other fields within the same model. Use annotate to do aggregation in the database.
Topic guide
Database access optimization
There are two reasons to use a column with unique or db_index when using get() to retrieve individual objects. First, the query will be quicker because of the underlying database index.
Topic guide
Database access optimization
Don’t retrieve things you don’t need
If you are going to need other data from the QuerySet, evaluate it immediately.
Topic guide
Database access optimization
Use bulk methods
When creating objects, where possible, use the bulk_create() method to reduce the number of SQL queries.
Topic guide
Database access optimization
Use bulk methods
Given a list or queryset of objects: The following example: …is preferable to: Note that there are a number of caveats to this method, so make sure it’s appropriate for your use case.
Topic guide
Making queries
Retrieving objects
The simplest way to retrieve objects from a table is to get all of them. To do this, use the all() method on a Manager: The all() method returns a QuerySet of all the objects in the database.
Topic guide
Making queries
Retrieving objects
Field lookups are how you specify the meat of an SQL WHERE clause. They’re specified as keyword arguments to the QuerySet methods filter(), exclude() and get(). Basic lookups keyword arguments take the form field__lookuptype=value. (That’s a double-underscore).
Topic guide
Performing raw SQL queries
Performing raw queries
Fields may also be left out: The Person objects returned by this query will be deferred model instances (see defer()). This means that the fields that are omitted from the query will be loaded on demand.
Topic guide
Creating forms from models
ModelForm
As you might expect, the ForeignKey and ManyToManyField model field types are special cases: ForeignKey is represented by django.forms.ModelChoiceField, which is a ChoiceField whose choices are a model QuerySet.
Topic guide
Django shortcut functions
get_object_or_404(klass,*args,**kwargs)[source] aget_object_or_404(klass,*args,**kwargs) : Asynchronous version: aget_object_or_404()
Topic guide
Django shortcut functions
get_list_or_404()
klass A Model class, a Manager, or a QuerySet instance from which to get the object. *args Q objects. **kwargs Lookup parameters, which should be in the format accepted by get() and filter().
Topic guide
Django shortcut functions
get_list_or_404(klass,*args,**kwargs)[source] aget_list_or_404(klass,*args,**kwargs) : Asynchronous version: aget_list_or_404()
Topic guide
Django shortcut functions
get_list_or_404()
klass A Model, Manager or QuerySet instance from which to get the list. *args Q objects. **kwargs Lookup parameters, which should be in the format accepted by get() and filter().