db.models.Field.get_prep_value()

get_prep_value(value) [source] value is the current value of the model’s attribute, and the method should return data in a format that has been prepared for use as a parameter in a query. See Converting Python objects to query values for usage.

admin.ModelAdmin.save_model()

ModelAdmin.save_model(request, obj, form, change) [source] The save_model method is given the HttpRequest, a model instance, a ModelForm instance and a boolean value based on whether it is adding or changing the object. Here you can do any pre- or post-save operations. For example to attach request.user to the object prior to saving: from django.contrib import admin class ArticleAdmin(admin.ModelAdmin): def save_model(self, request, obj, form, change): obj.user = request.user

views.generic.list.MultipleObjectMixin.get_paginate_by()

get_paginate_by(queryset) Returns the number of items to paginate by, or None for no pagination. By default this simply returns the value of paginate_by.

views.generic.dates.BaseDateListView.allow_empty

allow_empty A boolean specifying whether to display the page if no objects are available. If this is True and no objects are available, the view will display an empty page instead of raising a 404. This is identical to django.views.generic.list.MultipleObjectMixin.allow_empty, except for the default value, which is False.

views.generic.detail.SingleObjectTemplateResponseMixin

class django.views.generic.detail.SingleObjectTemplateResponseMixin A mixin class that performs template-based response rendering for views that operate upon a single object instance. Requires that the view it is mixed with provides self.object, the object instance that the view is operating on. self.object will usually be, but is not required to be, an instance of a Django model. It may be None if the view is in the process of constructing a new instance. Extends TemplateResponseMixin Met

Writing database migrations

This document explains how to structure and write database migrations for different scenarios you might encounter. For introductory material on migrations, see the topic guide. Data migrations and multiple databases When using multiple databases, you may need to figure out whether or not to run a migration against a particular database. For example, you may want to only run a migration on a particular database. In order to do that you can check the database connection’s alias inside a RunPython

utils.encoding.python_2_unicode_compatible()

python_2_unicode_compatible() [source] A decorator that defines __unicode__ and __str__ methods under Python 2. Under Python 3 it does nothing. To support Python 2 and 3 with a single code base, define a __str__ method returning text and apply this decorator to the class.

utils.translation.ugettext_noop()

ugettext_noop(message) Marks strings for translation but doesn’t translate them now. This can be used to store strings in global variables that should stay in the base language (because they might be used externally) and will be translated later.

core.files.storage.get_available_name()

get_available_name(name, max_length=None) Returns a filename that is available in the storage mechanism, possibly taking the provided filename into account. The name argument passed to this method will have already cleaned to a filename valid for the storage system, according to the get_valid_name() method described above. The length of the filename will not exceed max_length, if provided. If a free unique filename cannot be found, a SuspiciousFileOperation exception is raised. If a file wit

db.models.FileField.storage

FileField.storage A storage object, which handles the storage and retrieval of your files. See Managing files for details on how to provide this object. The default form widget for this field is a ClearableFileInput. Using a FileField or an ImageField (see below) in a model takes a few steps: In your settings file, you’ll need to define MEDIA_ROOT as the full path to a directory where you’d like Django to store uploaded files. (For performance, these files are not stored in the database.) De