Django Document--model field type (fieldtypes)

Source: Internet
Author: User
Tags file url
field type (types)

Autofield

It is a self-growing Integerfield field based on ID. Typically, you do not have to use the field directly. If you don't specify a primary key on another field, Django will automatically add the primary key field.

Bigintegerfield

A 64-bit integer, similar to Integerfield, ranging from 9223372036854775808 to 9223372036854775807. The default form widget is TextInput.

Booleanfield

A Boolean value (true/false) field.

The default form widget is Checkboxinput.

If you want to use NULL as a null value, you can use Nullbooleanfield.

Charfield

Class Charfield (max_length=none[, **options])

It is a string field that applies to both small and large strings.

For larger text, you should use TextField.

The default form widget is TextInput.

Charfield has a parameter that must be passed in: max_length, the maximum number of characters in a field. It acts on the database hierarchy and the Django data validation level.

Commaseparatedinterfield

Class Commaseparatedintegerfield (max_length=none[, **options])

It is used to hold a sequence of integers at a comma interval. As with Charfield, you must provide the Max_length parameter for it. Also pay attention to the limitations of different databases on max_length.

Datefield

Class Datefield ([Auto_now=false, Auto_now_add=false, **options])

This field uses Python's datetime.date instance to represent the date. Here is an additional optional parameter:

Datefield.auto_now: Every time you save an object, Django automatically sets the value of the field to the current time. Typically used to indicate "last modified" time. Note that you are using the current date instead of the default value, so

You cannot change the save time by overriding the default values.

Datefield.auto_now_add: When you create an object for the first time, Django automatically sets the value of the field to the current time, which is typically used to represent the object creation time. It uses the same current date, not the default value.

The default form widget is TextInput.

Note: When Auto_now or Auto_now_add is set to true, the fields will have Editable=true and blank=true settings.

Datetimefield

Class Datetimefield ([Auto_now=false, Auto_now_add=false, **options])

This field uses the Datetime.datetime instance to represent the date and time. The field is subject to the same parameters as the Datefield.

The default form widget is TextInput. The Django admin uses two textinput with JavaScript shortcut options to represent the date and time, respectively.

Decimalfield

Class Decimalfield (Max_digits=none, decimal_places=none[, **options])

It is a field that uses a decimal instance to represent a fixed-precision number of decimals. It has two parameters that must be:

Decimalfield.max_digits: Maximum number of digits allowed

Decimalfield.decimal_places: The maximum number of decimal places

For example, the maximum number to store is 999, and with two decimal digits, you can use:

Models. Decimalfield (..., max_digits=5, decimal_places=2)

To store a number that is about level 1 billion with 10 decimal digits, write this:

Models. Decimalfield (..., max_digits=19, decimal_places=10)

The default form widget is TextInput.

Emailfield

Class Emailfield ([Max_length=75, **options])

It is a charfield with email legality detection.

Note: The maximum length defaults to 75 and cannot store all rfc3696/5321-compatible email addresses. If you want to store all, please set

max_length=254. Setting to 75 is a legacy of history.

Filefield

Class Filefield (upload_to=none[, max_length=100, **options])

File Upload Field

Note: This field does not support Primary_key and unique parameters, otherwise a TypeError exception will be thrown.

It has a required parameter:

Filefield.upload_to

The local file system that is used to save the file. It determines the URL property of the file based on the Media_root setting.

The path can contain the time format string strftime (), can be uploaded to the file when the date/time (so that it will not appear in the upload file to fill a directory full of circumstances).

This parameter can also be a callable item, such as a function that can be called to get an upload path containing the file name. This callable item must accept two parameters.

and returns a path (with/slash) of the unix-style used to save the file. The two parameters are:

Instance: Defines the model instance of the current Filefield. More precisely, it is the model instance that is attached to the file.

In most cases, when you save the file, the model instance object is not saved to the database because it is likely that it will use the default Autofield, and it is not getting the primary key value from the database at this time.

FileName: The original name of the uploaded file. It is possible to use it when generating the final path.

There is also an optional parameter:

Filefield.storage

The object that is responsible for saving and retrieving the file.

The default form widget is Fileinput.

Note: Use Filefield or ImageField in model to follow these steps:

1. In the project settings file, you define Media_root and set its value to the full path of the directory where the files are uploaded. (For performance-based considerations, Django does not keep the file in the database).

Then define the Media_url and set its value to the URL that represents the directory.

To ensure that the account used by the Web server has write access to the directory.

2. Add Filefield or ImageField to the model and confirm that the UPLOAD_TO key is defined so that Django knows

Which subdirectory of Media_root to save the file.

3. Stored in the database is only the path to the file (and relative to the Media_root path). You may have thought of using

Django provides the URL for this handy property. For example, if your ImageField name is Mug_shot, then you can make a template

Used in

{{Object.mug_shot.url}}

You can get the full URL of the picture.

For example, suppose your media_root is set to '/home/media ' and upload_to is set to ' photos/%y/%m/%d '. In upload_to

The '%y/%m/%d ' is a strftime (), '%Y ' is a four-bit year, '%m ' is a two-bit month, '%d ' is a two-bit day. If you

A file was uploaded on January 15, 2007, and the file is stored in the/HOME/MEDIA/PHOTOS/2007/01/15 directory.

If you want to upload the file's local file name, file URL, or file size, you can use the name, URL, and Size property.

Note: When uploading a file, be wary of the location of the file and the type of file you are saving, so that you do this in order to avoid security vulnerabilities. For each upload

Files are verified so that you can ensure that the files you upload are the files you want. For example, if you blindly let someone upload a file without

To verify the uploaded file, if the directory where the file is saved is in the root directory of the Web server, in case someone uploads a CGI or PHP

Script, and then run the uploaded script by accessing the script URL, which is too dangerous. Do not let such a thing happen!

By default, the corresponding column of the Filefield instance in the database is varchar (100), and as with other fields, you can change the maximum length of the field by using the Max_length parameter.

Filefield and Fieldfile

Class Fieldfile

When you visit a model of the Filefield

field, a Fieldfile instance is obtained as a proxy to access the underlying file. Instances have several properties and methods that can be used to interact with file data.

Fieldfile.url

The URL () method of the underlying storage (Storage) class is called in a read-only manner to access the relative URL of the file.

Fieldfile.open (mode= ' RB ')

An open () method similar to Python.

Fieldfile.close ()

A close () method similar to Python.

Fieldfile.save (Name,content,save=true)

This method passes the filename and file contents to the field and then stores it to the model. The method requires two required parameters: name, the file names, the content, and the object that contains the contents of the file. The save parameter is optional, and the main

If the control file is modified, the instance is saved. The default is True. It is important to note that the content parameter is an instance of Django.core.files.File, not a python built-in File object. You can make

Use him to build a file from an existing Python file object, as follows:

From Django.core.files import File
# Open an existing file using Python ' s built-in Open ()
f = open ('/tmp/hello.world ')
MyFile = File (f)

or constructed from a string:

From django.core.files.base import ContentFile
MyFile = ContentFile ("Hello World")

Fieldfile.delete (Save=true)

Delete the file associated with this instance, and clear all properties of the field.

Note: When delete () is called, the method closes the file if the file is exactly open.

The save parameter is optional and controls whether the instance is saved after the file is deleted. The default is True.

It is important to note that when a model is deleted, the associated file is not deleted. If you want to delete these orphaned files, you need to handle them yourself (for example, you can run the command cleanup manually, or you can perform the cleanup commands regularly through Cron)

Filepathfield

Class Filepathfield (path=none[, Match=none, Recursive=false, max_length=100, **options])

It is a charfield that is used to select certain files in a directory under the file system. It has three proprietary parameters, and only the first parameter has a

The number is required:

Filepathfield.path

This parameter is required. It is the absolute path to a directory, which is the directory that Filepathfield uses to select files. Than

such as: "/home/images".

Filepathfield.match

Optional parameters. It is a regular expression string that Filepathfield uses to filter the name of the file, only the qualifying file appears in the

The file selection list. Note that the regular expression matches only the filename, not the file path. For example: "foo.*\.txt$" matches only the name of the

Foo23.txt and does not match bar.txt and foo23.gif.

Filepathfield.recursive

Optional parameters. Its value is True or False. The default value is False. It specifies whether to include subdirectories under path.

Filepathfield.allow_files

This item belongs to the Django1.5 new content. An optional parameter whose value is True or False. The default value is True. that specifies whether to include the file at the specified location. The item with Allow_folders must have one that is True.

Filepathfield.allow_folders

What's new in Django1.5. An optional parameter whose value is true or false. The default is False. that specifies whether to include the directory at the specified location. The item with Allow_files must have one that is True.

The match only matches the file name, not the file path, as mentioned earlier. So here's the example:

Filepathfield (path= "/home/images", match= "foo.*", Recursive=true)

Will match the/home/images/foo.gif, not the/home/images/foo/bar.gif. This is because match matches only the file name
(Foo.gif and Bar.gif).

By default, the corresponding column of the Filepathfield instance in the database is varchar (100). As with other fields, you can use the Max_length parameter to change the maximum length of a field.

Floatfield

Class Floatfield ([**options])

This field uses the float instance in Python to represent a floating-point number.

The default form widget is TextInput.

Please note the difference between Floatfield and Decimalfield.

ImageField

Class ImageField (upload_to=none[, Height_field=none, Width_field=none, Max_length=100,**options])

Like Filefield, it just verifies that the uploaded object is not a legitimate image file.

In addition to the parameters that are valid in Filefield, ImageField can also use File.height and File.width two properties.

It has two optional parameters:

Imagefield.height_field

The name of the field that holds the height of the picture. When you save an object, the picture file is scaled and converted according to the height set by the field.

Imagefield.width_field

The name of the field that holds the width of the picture. When you save an object, the picture file is scaled and converted according to the width set by the field.

By default, the ImageField instance corresponds to the varchar (100) column in the database. As with other fields, you can use the Max_length parameter to change the maximum length of a field.

Integerfield

Class Integerfield ([**options])

Integer Number field. The default form widget is TextInput.

Ipaddressfield

Class Ipaddressfield ([**options])

Represents the IP address field as a string, such as "192.0.2.30". The default form widget is TextInput.

Genericipaddressfield

Class Genericipaddressfield ([**options])

Django1.4 added.

Represents a IP4 or IP6 address field as a string (such as "192.0.2.30" or "2a02:42fe::4"). The default form widget is TextInput.

The address format of the IPV6 follows RFC 4291 section 2.2. For example, if the address is actually a IPv4 address, the latter 32 bits can be expressed in 10 decimal digits, such as ":: ffff:192.0.2.0".

2001:0::0:01 can be written as 2001::1, and:: ffff:0a0a:0a0a can be written:: ffff:10.10.10.10. The letters are lowercase.

Genericipaddressfield.protocol

Verify the validity of the input protocol. The default value is ' both ', which is IPv4 or IPv6. The item is not case-sensitive.

Genericipaddressfield.unpack_ipv4

Explains the address of the IPV4 map, such as: ffff:192.0.2.1. If this option is enabled, the address will be interpreted as 192.0.2.1. The default is forbidden. It can only be enabled if protocol is set to ' both '.

Nullbooleanfield

Class Nullbooleanfield ([**options])

Similar to Booleanfield, but with one more NULL option. This field is recommended instead of Booleanfield with the null=true option.

The default form widget is Nullbooleanselect.

Positiveintegerfield

Class Positiveintegerfield ([**options])

Similar to Integerfield, but field values must be non-negative.

Positivesmallintegerfield

Class Positivesmallintegerfield ([**options])

Similar to Positiveintegerfield, but with a small range of values, limited by database settings.

Slugfield

Class Slugfield ([Max_length=50, **options])

Slug is a news term that refers to a short label for an event. It can only consist of letters, numbers, underscores, or hyphens. In the case of a reward, it is used as part of the URL.

Similar to Charfield, you can specify Max_length (be aware of database compatibility and the max_length mentioned in this section). If you do not specify Max_length, Django will have a default field length of 50.

The field is automatically set Field.db_index to True.

It is useful to automatically populate the Slug field based on the values of other fields. You can use Prepopulated_fields in the Django admin background to do this.

Smallintegerfield

Class Smallintegerfield ([**options])

Similar to Integerfield, but with a small range of values, limited by the database.

TextField

Class TextField ([**options])

A large text field. The default form widget is textarea.

Timefield

Class Timefield ([Auto_now=false, Auto_now_add=false, **options])

This field uses Python's Datetime.time instance to represent time. It and Datefield accept the same auto-filled parameters.

The default form widget is TextInput.

Urlfield

Class Urlfield ([max_length=200, **options])

Save the Charfield of the URL.

As with all Charfield subclasses, Urlfield accepts the optional max_length parameter, which is the default value of 200.

The above is the content of the Django Document--model field type (fieldtypes), more related articles please pay attention to topic.alibabacloud.com (www.php.cn)!

  • Related Article

    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.