db.models.ForeignKey.limit_choices_to

ForeignKey.limit_choices_to Sets a limit to the available choices for this field when this field is rendered using a ModelForm or the admin (by default, all objects in the queryset are available to choose). Either a dictionary, a Q object, or a callable returning a dictionary or Q object can be used. For example: staff_member = models.ForeignKey( User, on_delete=models.CASCADE, limit_choices_to={'is_staff': True}, ) causes the corresponding field on the ModelForm to list only Us

admin.AdminSite.has_permission()

AdminSite.has_permission(request) [source] Returns True if the user for the given HttpRequest has permission to view at least one page in the admin site. Defaults to requiring both User.is_active and User.is_staff to be True.

db.models.SlugField

class SlugField(max_length=50, **options) [source] Slug is a newspaper term. A slug is a short label for something, containing only letters, numbers, underscores or hyphens. They’re generally used in URLs. Like a CharField, you can specify max_length (read the note about database portability and max_length in that section, too). If max_length is not specified, Django will use a default length of 50. Implies setting Field.db_index to True. It is often useful to automatically prepopulate a Slu

db.models.Func

class Func(*expressions, **extra) [source] function A class attribute describing the function that will be generated. Specifically, the function will be interpolated as the function placeholder within template. Defaults to None. template A class attribute, as a format string, that describes the SQL that is generated for this function. Defaults to '%(function)s(%(expressions)s)'. If you’re constructing SQL like strftime('%W', 'date') and need a literal % character in the query, quadr

test.runner.DiscoverRunner.build_suite()

DiscoverRunner.build_suite(test_labels, extra_tests=None, **kwargs) Constructs a test suite that matches the test labels provided. test_labels is a list of strings describing the tests to be run. A test label can take one of four forms: path.to.test_module.TestCase.test_method – Run a single test method in a test case. path.to.test_module.TestCase – Run all the test methods in a test case. path.to.module – Search for and run all tests in the named Python package or module. path/to/direct

db.models.Func.as_sql()

as_sql(compiler, connection, function=None, template=None, arg_joiner=None, **extra_context) [source] Generates the SQL for the database function. The as_vendor() methods should use the function, template, arg_joiner, and any other **extra_context parameters to customize the SQL as needed. For example: class ConcatPair(Func): ... function = 'CONCAT' ... def as_mysql(self, compiler, connection): return super(ConcatPair, self).as_sql( compiler, connection,

admin.ModelAdmin.formfield_overrides

ModelAdmin.formfield_overrides This provides a quick-and-dirty way to override some of the Field options for use in the admin. formfield_overrides is a dictionary mapping a field class to a dict of arguments to pass to the field at construction time. Since that’s a bit abstract, let’s look at a concrete example. The most common use of formfield_overrides is to add a custom widget for a certain type of field. So, imagine we’ve written a RichTextEditorWidget that we’d like to use for large tex

forms.TimeField

class TimeField(**kwargs) [source] Default widget: TextInput Empty value: None Normalizes to: A Python datetime.time object. Validates that the given value is either a datetime.time or string formatted in a particular time format. Error message keys: required, invalid Takes one optional argument: input_formats A list of formats used to attempt to convert a string to a valid datetime.time object. If no input_formats argument is provided, the default input formats are: '%H:%M:%S',

Integrating Django with a legacy database

While Django is best suited for developing new applications, it’s quite possible to integrate it into legacy databases. Django includes a couple of utilities to automate as much of this process as possible. This document assumes you know the Django basics, as covered in the tutorial. Once you’ve got Django set up, you’ll follow this general process to integrate with an existing database. Give Django your database parameters You’ll need to tell Django what your database connection parameters are

db.models.expressions.RawSQL

class RawSQL(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. For example: >>> from django.db.models.expressions import RawSQL >>> queryset.annotate(val=RawSQL("select col from sometable where othercol = %s", (someparam,))) These extra lookups may not be portable to different database engines (because you’re explicitly writing SQL code) and violate the DRY pr