Django Model Field Reference

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Parameters

ParameterDetails
nullIf true, empty values may be stored as null in the database
blankIf true, then the field will not be required in forms. If fields are left blank, Django will use the default field value.
choicesAn iterable of 2-element iterables to be used as choices for this field. If set, field is rendered as a drop-down in the admin. [('m', 'Male'),('f','Female'),('z','Prefer Not to Disclose')]. To group options, simply nest the values: [('Video Source',((1,'YouTube'),(2,'Facebook')),('Audio Source',((3, 'Soundcloud'),(4, 'Spotify'))]
db_columnBy default, django uses the field name for the database column. Use this to provide a custom name
db_indexIf True, an index will be created on this field in the database
db_tablespaceThe tablespace to use for this field's index. This field is only used if the database engine supports it, otherwise its ignored.
defaultThe default value for this field. Can be a value, or a callable object. For mutable defaults (a list, a set, a dictionary) you must use a callable. Due to compatibility with migrations, you cannot use lambdas.
editableIf False, the field is not shown in the model admin or any ModelForm. Default is True.
error_messagesUsed to customize the default error messages shown for this field. The value is a dictionary, with the keys representing the error and the value being the message. Default keys (for error messages) are null, blank, invalid, invalid_choice, unique and unique_for_date; additional error messages may be defined by custom fields.
help_textText to be displayed with the field, to assist users. HTML is allowed.
on_deleteWhen an object referenced by a ForeignKey is deleted, Django will emulate the behavior of the SQL constraint specified by the on_delete argument. This is the second positional argument for both ForeignKey and OneToOneField fields. Other fields do not have this argument.
primary_keyIf True, this field will be the primary key. Django automatically adds a primary key; so this is only required if you wish to create a custom primary key. You can only have one primary key per model.
uniqueIf True, errors are raised if duplicate values are entered for this field. This is a database-level restriction, and not simply a user-interface block.
unique_for_dateSet the value to a DateField or DateTimeField, and errors will be raised if there are duplicate values for the same date or date time.
unique_for_monthSimilar to unique_for_date, except checks are limited for the month.
unique_for_yearSimilar to unique_for_date, except checks are limited to the year.
verbose_nameA friendly name for the field, used by django in various places (such as creating labels in the admin and model forms).
validatorsA list of validators for this field.

Remarks

  • You can write your own fields if you find it necessary
  • You can override functions of the base model class, most commonly the save() function


Got any Django Question?