Django document -- Model Field Options)

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.