Field options¶
Each field takes a certain set of field-specific arguments (documented in the model field
reference). For example, CharField (and its subclasses) require a max_length argument which
specifies the size of the VARCHAR database field used to store the data.
There’s also a set of common arguments available to all field types. All are optional. They’re
fully explained in the reference, but here’s a quick summary of the most often-used ones:
null
If True, Django will store empty values as NULL in the database. Default is False.
blank
If True, the field is allowed to be blank. Default is False.
Note that this is different than null. null is purely database-related, whereas blank is
validation-related. If a field has blank=True, form validation will allow entry of an empty
value. If a field has blank=False, the field will be required.
choices
A sequence of 2-tuples to use as choices for this field. If this is given, the default form
widget will be a select box instead of the standard text field and will limit choices to the
choices given.
A choices list looks like this:
YEAR_IN_SCHOOL_CHOICES = [
('FR', 'Freshman'),
('SO', 'Sophomore'),
('JR', 'Junior'),
('SR', 'Senior'),
('GR', 'Graduate'),
Note
A new migration is created each time the order of choices changes.