djangodocs.org

477 sections across all versions Narrow to Django 6.0 (current) →

in

Reference QuerySet API reference QuerySet APIField lookups

In a given iterable; often a list, tuple, or queryset. It’s not a common use case, but strings (being iterables) are accepted.

4.2 5.0 5.1 5.2 6.0

range

Reference QuerySet API reference QuerySet APIField lookups

Range test (inclusive). Example: SQL equivalent: You can use range anywhere you can use BETWEEN in SQL — for dates, numbers and even characters.

4.2 5.0 5.1 5.2 6.0

regex

Reference QuerySet API reference QuerySet APIField lookups

Case-sensitive regular expression match. The regular expression syntax is that of the database backend in use.

4.2 5.0 5.1 5.2 6.0

include

Reference Built-in template tags and filters Built-in tag reference

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.

4.2 5.0 5.1 5.2 6.0

Retrieving objects

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.

4.2 5.0 5.1 5.2 6.0

Limiting QuerySets

Topic guide Making queries Retrieving objects

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.

4.2 5.0 5.1 5.2 6.0

Field lookups

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).

4.2 5.0 5.1 5.2 6.0

Performing raw queries

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.

4.2 5.0 5.1 5.2 6.0

Savepoints in SQLite

Topic guide Database transactions Database-specific notes

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.

4.2 5.0 5.1 5.2 6.0

Using a callable

Topic guide Managing files File storage

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.

4.2 5.0 5.1 5.2 6.0

Field types

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.

5.2 6.0

A full example

Topic guide Creating forms from models ModelForm

Consider this set of models: With these models, the ModelForm subclasses above would be roughly equivalent to this (the only difference being the save() method, which we’ll discuss in a moment.):

4.2 5.0 5.1 5.2 6.0

TransactionTestCase

Topic guide Testing tools Provided test case classes

classTransactionTestCase[source] TransactionTestCase inherits from SimpleTestCase to add some database-specific features: Resetting the database to a known state at the end of each test to ease testing and using the ORM. Database fixtures. Test skipping based on database backend features.

4.2 5.0 5.1 5.2 6.0

Field name restrictions

Topic guide Models Fields

Django places some restrictions on model field names: A field name cannot be a Python reserved word, because that would result in a Python syntax error.

4.2 5.0 5.1 5.2 6.0

Field types

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.

4.2 5.0 5.1

Database setup

Tutorial Writing your first Django app, part 2

Now, open up mysite/settings.py. It’s a normal Python module with module-level variables representing Django settings. By default, the DATABASES configuration uses SQLite. If you’re new to databases, or you’re just interested in trying Django, this is the easiest choice.

4.2 5.0 5.1 5.2 6.0