A summary of the field types of the Django model

Source: Internet
Author: User

Field TypesCommon parameters: NULLIf set to True, Django holds a NULL to the database field. The default is False. BlankIf set to True, this field allows blank (white space), which defaults to False.
ChoicesA tuple or list of 2 tuples, if executed choices, the Django admin will fill in the field with a selection box instead of the standard text box.
Year_in_school_choices = (U ' FR ', U ' Freshman '), (U ' so ', U ' sophomore '), (U ' JR ', U ' Junior '), (U ' SR ', U ' Senior ') , (U ' GR ', U ' Graduate '),)
2 The first element of the tuple is the data to be stored in the database, and the second element is the data displayed by the admin interface. field using the choices parameter in its model example, you can use the "Get_field name _display" method to display the choices display string (which is the second data for the 2 tuple). Example
From django.db Import models class person (models. Model): Gender_choices = ((U ' M ', U ' Male '), (U ' F ', U ' Female '),) name = models. Charfield (max_length=60) gender = models. Charfield (max_length=2, choices=gender_choices)
>>> p = person (name= "Fred flinstone", gender= "M") >>> P.save () >>> P.genderu ' m ' >>> P.get_gender_display () u ' Male '
defaultThe default value of field, which can be invoked with the callable object (a callable object) and, if the callable object is used, invokes the callable object each time a new object of this model is created. Common as datatime. Help_textThe value of Help_text can be displayed in the admin form, but even if you do not use admin, you can use it as a descriptive document. Primary_keyIf true, this field is the primary key for this model. UniqueIf true, this field must be unique in this table. Verbose_nameVerbose, the detailed meaning. Verbose_name, it can be understood as a detailed name. In addition to ForeignKey, Manytomanyfield and Onetoonefield, each type of field has an optional first position parameter-a detailed name. If no detailed name is given, Django will automatically replace it with the field's property name. An underscore is converted to a space during the substitution process. In this field, the details of the name are "person ' s first Name":
First_Name = models. Charfield ("person ' s first name", max_length=30)
In the following fields, the detailed name of First_Name is "first name":
First_Name = models. Charfield (max_length=30)
ForeignKey, Manytomanyfield and Onetoonefield require the first parameter to be the class of the model, so you need to use the Verbose_name keyword parameter, such as:
Poll = models. ForeignKey (Poll, verbose_name= "the related Poll") sites = models. Manytomanyfield (Site, verbose_name= "List of sites") Place = models. Onetoonefield (place, verbose_name= "related place")
Django will automatically capitalize the first letter of the verbose_name when needed. The original Verbose_name field is for ForeignKey, Manytomanyfield and Onetoonefield three kinds of relationship prepared AH! Common filed Types 1, AutofieldIf no primary key is indicated, a self-increment primary key is generated. 2, BigintegerfieldAn integer value of 64 bits, from -2^63 (-9223372036854775808) to 2^63-1 (9223372036854775807). 3, BinaryfieldStores raw binary data, only byte allocations are supported. Limited functionality. 4, BooleanfieldBoolean and Nullbooleanfield are different, true/false, this type is not allowed to appear null. 5, CharfieldString, which is typically written to the Max_length parameter at creation time. 6, CommaseparatedintegerfieldComma-delimited integers, max_length parameters should be required, given the portability of the database. Original explanation: A field of integers separated by commas. As in Charfield, the max_length argument are required and the note about database portability mentioned there should be Hee Ded. 7, DatefieldTime, corresponding to Python's datetime.date, additional parameters: Datefield.auto_now Indicates whether the time is changed on each modification, Datefield.auto_now_add indicates when the time is created, It is generally important for a database to have such a field record the time of the last change in the creation field. About time, suggest timestamp, of course, Python is still datetime. 8, DatetimefieldCorresponding to Python's datetime.datetime, refer to parameter (7). 9, DecimalfieldA fixed-precision decimal number that is commonly used to store amount-related data. For the Python decimal, the additional parameters include Decimalfield.max_digits and decimalfield.decimal_places, which is still a reference to the MySQL decimal type, HTTP// Database.51cto.com/art/201005/201651.htm For example: Price = models. Decimalfield (max_digits=8,decimal_places=2) 10, EmailfieldString that will check if it is a legitimate email address 11, FilefieldClass Filefield ([Upload_to=none, max_length=100, **options]) Save the file, parameter upload_to required in some older versions prior to 1.7 12, FloatfieldFloating-point numbers, required parameters: max_digits, number length, decimal_places, number of significant digits. 13, ImageFieldClass ImageField ([Upload_to=none, Height_field=none, Width_field=none, max_length=100, **options]) picture file type, Inherits all the properties and methods of the Filefield. Parameters except Upload_to, there are Height_field,width_field and other properties. 14, IntegerfieldThe value range of [ -2147483648,2147483647] is safe for Django-supported databases. 15, IpaddressfieldThe IP address in dotted decimal notation, such as 10.0.0.1 16, GenericipaddressfieldIP v4 and IP V6 address representations, IPv6 follows RFC 4291section 2.2, 17, NullbooleanfieldA Boolean type that can contain null values, equivalent to the Booleanfield that set the null=true. 18, PositiveintegerfieldA positive integer or 0 type with a value range of [0, 2147483647] 19, PositivesmallintegerfieldA positive short integer or 0 type, similar to Positiveintegerfield, where the range of values depends on database attributes, [0, 32767] is safe for the database that Django supports. 20, SlugfieldA string that can contain only letters, numbers, underscores, and hyphens, and is usually used for the URLs representation. An optional parameter, Max_length=50,prepopulate_from, is used to indicate an optional value in the admin form. Db_index, which is true by default. 21, SmallintegerfieldThe small integer field, similar to Integerfield, depends on the database attribute, and the range of values [-32768, 32767] is secure for the database that Django supports. 22, TextFieldText type 23, TimefieldTime, corresponding to Python's datetime.time 24, UrlfieldThe string that stores the URL, the default length of 200;verify_exists (True), checks for URL availability. 25,Filepathfield Class Filepathfield (path=none[, Match=none, Recursive=false, max_length=100, **options]) are similar to Charfield, However, the value is limited to the file name within the specified path, and the path parameter is required. Reference: https://docs.djangoproject.com/en/dev/ref/models/fields/#django. db.models.filefieldhttp:// jianlee.ylinux.org/computer/python/django_models.htmlhttp://luo3380.blog.163.com/blog/static/ 12043208620131139254121/http://www.zijin5.com/django-model-yi/
Top
0

A summary of the field types of the Django model

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.