Reference Query Expressions
classRawSQL(sql,params,output_field=None)[source] Sometimes database expressions can’t easily express a complex WHERE clause. In these edge cases, use the RawSQL expression.
477 sections across all versions Narrow to Django 6.0 (current) →
Reference Query Expressions
classRawSQL(sql,params,output_field=None)[source] Sometimes database expressions can’t easily express a complex WHERE clause. In these edge cases, use the RawSQL expression.
Reference Model field reference
Field.choices[source] A mapping or iterable in the format described below to use as choices for this field.
Reference QuerySet API reference
exclude(*args,**kwargs) Returns a new QuerySet containing objects that do not match the given lookup parameters. The lookup parameters (**kwargs) should be in the format described in Field lookups below.
Reference QuerySet API reference
union(*other_qs,all=False) Uses SQL’s UNION operator to combine the results of two or more QuerySets. For example: The UNION operator selects only distinct values by default. To allow duplicate values, use the all=True argument.
Reference QuerySet API reference
latest(*fields) alatest(*fields) Asynchronous version: alatest() Returns the latest object in the table based on the given field(s).
Reference QuerySet API reference
Exact match. If the value provided for comparison is None, it will be interpreted as an SQL NULL (see isnull for more details). Key and index lookups are exceptions: they interpret None as JSON null instead.
Reference QuerySet API reference
In a given iterable; often a list, tuple, or queryset. It’s not a common use case, but strings (being iterables) are accepted.
Reference QuerySet API reference
Range test (inclusive). Example: SQL equivalent: You can use range anywhere you can use BETWEEN in SQL — for dates, numbers and even characters.
Reference QuerySet API reference
Case-sensitive regular expression match. The regular expression syntax is that of the database backend in use.
Reference The Django template language: for Python programmers
staticEngine.get_default()[source] : Returns the underlying Engine from the first configured DjangoTemplates engine. Raises ImproperlyConfigured if no engines are configured. Engine.from_string(template_code)[source] : Compiles the given template code and returns a Template object.
Reference Built-in template tags and filters
Loads a template and renders it with the current context. This is a way of “including” other templates within a template. The template name can either be a variable or a hardcoded (quoted) string, in either single or double quotes.
Topic guide Making queries
To retrieve objects from your database, construct a QuerySet via a Manager on your model class. A QuerySet represents a collection of objects from your database. It can have zero, one or many filters.
Topic guide Making queries
Use a subset of Python’s array-slicing syntax to limit your QuerySet to a certain number of results. This is the equivalent of SQL’s LIMIT and OFFSET clauses.
Topic guide Making queries
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 Making queries
The field lookups that equate to LIKE SQL statements (iexact, contains, icontains, startswith, istartswith, endswith and iendswith) will automatically escape the two special characters used in LIKE statements – the percent sign and the underscore.
Topic guide Performing raw SQL queries
The raw() manager method can be used to perform raw SQL queries that return model instances: Manager.raw(raw_query,params=(),translations=None) This method takes a raw SQL query, executes it, and returns a django.db.models.query.RawQuerySet instance.
Topic guide Performing raw SQL queries
Sometimes even Manager.raw() isn’t quite enough: you might need to perform queries that don’t map cleanly to models, or directly execute UPDATE, INSERT, or DELETE queries.
Topic guide Database transactions
While SQLite supports savepoints, a flaw in the design of the sqlite3 module makes them hardly usable. When autocommit is enabled, savepoints don’t make sense. When it’s disabled, sqlite3 commits implicitly before savepoint statements.
Topic guide Managing files
You can use a callable as the storage parameter for FileField or ImageField. This allows you to modify the used storage at runtime, selecting different storages for different environments, for example.
Topic guide Creating forms from models
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.