How-to
How to write custom lookups
The AbsoluteValue example we discussed previously is a transformation which applies to the left-hand side of the lookup. There may be some cases where you want the transformation to be applied to both the left-hand side and the right-hand side.
How-to
How to create custom model fields
When planning your Field subclass, first give some thought to which existing Field class your new field is most similar to. Can you subclass an existing Django field and save yourself some work?
Internals
Django Deprecation Timeline
See the Django 1.6 release notes for more details on these changes. django.contrib.comments will be removed.
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.
Tutorial
Writing your first Django app, part 4
Let’s update our poll detail template (“polls/detail.html”) from the last tutorial, so that the template contains an HTML <form> element: polls/templates/polls/detail.html A quick rundown: The above template displays a radio button for each question choice.
Reference
Applications
Application configuration
AppConfig.name : Full Python path to the application, e.g. 'django.contrib.admin'. AppConfig.label : Short name for the application, e.g. 'admin' AppConfig.verbose_name : Human-readable name for the application, e.g. “Administration”. AppConfig.path : Filesystem path to the application directory, e.g. '/usr/lib/pythonX.Y/dist-packages/django/contrib/admin'.
Reference
The Django admin site
ModelAdmin objects
ModelAdmin.filter_horizontal : By default, a ManyToManyField is displayed in the admin site with a <select multiple>. However, multiple-select boxes can be difficult to use when selecting many items.
Reference
django-admin and manage.py
Available commands
django-admin dbshell Runs the command-line client for the database engine specified in your ENGINE setting, with the connection parameters specified in your USER, PASSWORD, etc., settings. For PostgreSQL, this runs the psql command-line client.
Reference
django-admin and manage.py
Available commands
django-admin shell Starts the Python interactive interpreter. All models from installed apps are automatically imported into the shell environment. Models from apps listed earlier in INSTALLED_APPS take precedence.
Reference
Detailed form API reference. For introductory material, see the Working with forms topic guide.
Reference
Widgets
Base widget classes
classMultiWidget(widgets,attrs=None)[source] : A widget that is composed of multiple widgets. MultiWidget works hand in hand with the MultiValueField.
Reference
Conditional Expressions
Advanced queries
What if we want to find out how many clients there are for each account_type?
Reference
Model field reference
Field options
The key of the mapping is the name to apply to the group and the value is the choices inside that group, consisting of the field value and a human-readable name for an option.
Reference
Model instance reference
Saving objects
You may have noticed Django database objects use the same save() method for creating and changing objects. Django abstracts the need to use INSERT or UPDATE SQL statements.
Reference
QuerySet API reference
Internally, a QuerySet can be constructed, filtered, sliced, and generally passed around without actually hitting the database. No database activity actually occurs until you do something to evaluate the queryset. You can evaluate a QuerySet in the following ways: Iteration.
Reference
QuerySet API reference
QuerySet APIMethods that return new QuerySets
values_list(*fields,flat=False,named=False) This is similar to values() except that instead of returning dictionaries, it returns tuples when iterated over.
Reference
QuerySet API reference
QuerySet APIMethods that do not return QuerySets
explain(format=None,**options) aexplain(format=None,**options) Asynchronous version: aexplain() Returns a string of the QuerySet’s execution plan, which details how the database would execute the query, including any indexes or joins that would be used.
Topic guide
Django’s cache framework
If you’re after even more control, you can also cache template fragments using the cache template tag. To give your template access to this tag, put {% load cache %} near the top of your template.
Topic guide
Aggregation
Aggregations and other QuerySet clausesvalues()
Fields that are mentioned in the order_by() part of a queryset are used when selecting the output data, even if they are not otherwise specified in the values() call.
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.