Create a simple Model
Class Person (models. model): GENDER_CHOICES = (1, 'male'), (2, 'female '),) name = models. charField (max_length = 30, unique = True, verbose_name = 'last name') birthday = models. dateField (blank = True, null = True) gender = models. integerField (choices = GENDER_CHOICES) account = models. integerField (default = 0)
Blank
If it is set to True, the field can be blank. When it is set to False, the field is required. Character fields CharField and TextField use empty strings to store null values.
Null
When set to True, django uses Null to store Null values. The date, time, and number fields do not accept empty strings. Therefore, when you set IntegerField and the DateTimeField field can be null, you must set blank and null to True.
If you want to set BooleanField to null, you can use the NullBooleanField field.
Max_length
Set the maximum length for the CharField field.
Choices
Choices whose elements are 2-tuples sequences (list or tuple. The first element of 2-tuple is stored in the database. The second element can be obtained by the get_FOO_display method.
>>>p=Person(name='Sam',gender=1)>>>p.save()>>>p.gender1>>>p.get_gender_display()u'Male'
If there are too many choices options, you 'd better consider using ForiegnKey.
Default
Set the default value for the field.
The default value cannot be a variable object (Model instance, list, set, etc.). As a reference to the same instance, this object will be used as the default value of all new model instances. Instead, encapsulate the required default values in a callable object. For example, if you have a custom JSONField and want to specify a dictionary as the default dictionary, use a lambda expression as follows:
contact_info = JSONField("ContactInfo", default=lambda:{"email": "to1@example.com"})
Verbose_name
Set the display name of this field on the admin interface.
Unique
Set to True. This field must be unique in the database.
>>>p=Person(name='Sam',gender=1)>>>p.save()>>>p=Person(name='Sam',gender=2)>>>p.save()IntergrityError: column name is not unique
Primary_key
If set to True, this field becomes the primary key of the Model. Generally, django automatically adds an IntegerField named id to the Model as the primary key.