Django Document--model field options (fieldoptions)

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= ' name ')
Birthday=models. Datefield (Blank=true,null=true)
Gender=models. Integerfield (choices=gender_choices)
Account=models. Integerfield (default=0)

Blank

When set to true, the field can be empty. When set to False, the field is required to be filled in. Character Fields Charfield and TextField use empty strings to store null values.

Null

When set to True, Django uses NULL to store null values. Date, time, and numeric fields do not accept empty strings. So setting the Integerfield,datetimefield type field can be empty, you need to set Blank,null to true.

If you want to set Booleanfield to empty, you can choose the Nullbooleanfield type field.

Max_length

Set the maximum length for the Charfield type field.

Choices

A sequence of elements 2-tuples (list or tuple) as the choices of the field. The first element of the 2-tuple is stored in the database, and the second element can be obtained by the Get_foo_display method.

>>>p=person (name= ' Sam ', gender=1)
>>>p.save ()
>>>p.gender
1
>>>p.get_gender_display ()
U ' Male '

If you have too many options for choices, you might want to consider using Foriegnkey.

Default

Set a default value for the field.

The default value cannot be a mutable object (model instance, list, collection, etc.), and as a reference to the same instance, the object will be used as the default value in 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, use a lambda expression as follows:

Contact_info = Jsonfield ("ContactInfo", default=lambda:{"email": "To1@example.com"})

Verbose_name

Sets the display name for 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. In general, Django will automatically add a Integerfield field called ID to the model as the primary key.

The above is the content of the Django Document--model field option (fieldoptions), more related articles please follow topic.alibabacloud.com (www.php.cn)

  • 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.