Django Model Field Type

Source: Internet
Author: User

Django ModelField Type:

Autofield
An integerfield that automatically increases when a record is added. you do not need to use this field directly. If you do not specify a primary key, the system automatically adds a primary key field to your model. (See _ automatic primary key fields)
Booleanfield
A true/false field. Admin uses checkbox to represent such fields.
Charfield

String Field, used for short strings.

To save a large amount of text, use textfield.

Admin uses <input type = "text"> to represent such fields (single row input ).

Charfield must have a maxlength parameter, which is used to limit the maximum number of characters allowed by this field from the database layer and Django validation layer.

 

Commaseparatedintegerfield
Used to store comma-separated integer values. Similar to charfield, The maxlength parameter must exist.
Datefield

A date field. There are the following additional optional parameters:

Argument Description
Auto_now When an object is saved, the value of this field is automatically set to the current time. It is usually used to indicate the "Last-modified" timestamp.
Auto_now_add When an object is created for the first time, the value of this field is automatically set to the current time, which usually indicates the object creation time.

Admin uses a text box <input type = "text"> to represent the field data (with a javascript calendar and a "today" shortcut key.

 

Datetimefield

A date and time field. Similar to datefield, the same additional options are supported.

Admin uses the preceding text box <input type = "text"> to indicate the field order (with JavaScript shortcuts ).

 

Emailfield
A charfield with email validity check does not accept the maxlength parameter.
Filefield

A file upload field.

A required parameter: upload_to, a local file system path used to save the uploaded file. this path must contain strftime formatting, which will be replaced by the date/time of the uploaded file (so that uploaded files don't fill up the given directory ).

Admin uses a ''<input type =" file ">'' part to indicate the data stored in this field (a file upload part ).

To use filefield or imagefield in a model, follow these steps:

  1. In your settings file, define a complete path to media_root so that Django can save the upload file here. (For performance considerations, these files are not stored in the database .) define media_url as the public URL of the directory. make sure that the directory is writable to the Web server user account.
  2. Add filefield or imagefield to your model and make sure that the upload_to option is defined to tell Django which subdirectory of media_root is used to save the uploaded file.
  3. What you want to save in your database is the file path (relative to media_root ). out of habit, you must really want to use the GET _ <fieldname> _ URL function provided by Django. for example, if your imagefield is called mug_shot, you can use {object. get_mug_shot_url} to obtain the absolute path of the image.
Filepathfield

The optional item is the file name under a specific directory. Three special parameters are supported, and the first is required.

Parameters Description
Path Required parameter. The absolute file system path of a directory. filepathfield accordingly to obtain the optional project. Example: "/home/images ".
Match Optional. A regular expression. As a string, filepathfield uses it to filter file names. note that this regular expression will only apply to base filename instead of the full path name. example: "foo. */. TXT ^ ", will match the file foo23.txt but does not match bar.txt or foo23.gif.
Recursive Optional parameter. either true or false. The default value is false. whether to include all subdirectories under path.

These three parameters can be used at the same time.

I have told you that match applies only to base filename, not the full name of the path. So, in this example:

FilePathField(path="/home/images", match="foo.*", recursive=True)

... Will match/home/images/foo.gif but not/home/images/Foo/bar.gif


Floatfield

A floating point number. RequiredProvide twoParameters:

Parameters Description
Max_digits Total digits (excluding decimal points and symbols)
Decimal_places Decimal places

For example, to save the maximum value of 999 (two digits after the decimal point), you need to define the field as follows:

models.FloatField(..., max_digits=5, decimal_places=2)

To save the maximum value of 1 million (10 digits after the decimal point), you need to define it as follows:

models.FloatField(..., max_digits=19, decimal_places=10)

Admin uses a text box (<input type = "text">) to indicate the data stored in this field.


Imagefield

It is similar to filefield, but it is necessary to verify whether the uploaded object is a valid image. it has two optional parameters: height_field and width_field. If the two parameters are provided, the image is saved according to the provided height and width.

This field requires Python imaging library.

 

Integerfield

Used to save an integer.

Admin uses ''<input type =" text ">'' to indicate the data stored in this field (a single row editing box)

 

Ipaddressfield

An IP address in string format (I. e. "24.124.1.30 ").

Admin uses ''<input type =" text ">'' to indicate the data stored in this field (a single row editing box)

 

Nullbooleanfield

Similar to booleanfield, but null is allowed as one of the options. We recommend that you use this field instead of the booleanfield plus null = true option.

Admin uses a selection box <SELECT> (three optional values: "unknown", "yes", and "no") to represent the field data.

 

Phonenumberfield
A charfield ''(Format: ''xxx-XXX-XXXX) with valid American phone number verification ).
Positiveintegerfield
This is similar to integerfield, but the value range is not a negative integer (This field should allow 0 value... so it is not good to get the field name and the unsigned integer is correct ).
Positivesmallintegerfield
Similar to positiveintegerfield, the value range is small (Database-related)
Slugfield

"Slug" is a newspaper term. slug is a small sign (short sign) of something that only contains letters, numbers, underscores, and hyphens. They are generally used for URLs.

If you use the Django development version, you can specify maxlength. If maxlength is not specified, Django will use the default length: 50. In the previous Django version, there is no way to change the length of 50.

This implies db_index = true.

It accepts an additional parameter: prepopulate_from, which is a list of fields from which to auto-populate the slug, via JavaScript, In the object's admin form:

models.SlugField(prepopulate_from=("pre_name", "name"))

Prepopulate_from does not accept datetimefields.

Admin uses a ''<input type =" text ">'' to indicate the data of the slugfield field (a single row editing box)

 

Smallintegerfield
Similar to integerfield, but only integers in a certain value range are allowed. (dependent on the database)
Textfield

A large text field.

Admin uses a <textarea> (text area) to represent the field data (a multi-row edit box ).

 

Timefield

A time. Accepts the same auto-Population Options as datefield and datetimefield.

Admin uses a <input type = "text"> text box to indicate the data stored in this field (add some JavaScript shortcuts ).

 

Urlfield

Used to save the URL. If the verify_exists parameter is true (default), the given URL will be checked in advance (that is, whether the URL is effectively loaded and no 404 response is returned ).

Admin uses a <input type = "text"> text box to indicate the data stored in this field (a single row editing box)

 

Usstatefield

The abbreviation of a two-letter U.S. state.

Admin uses a <input type = "text"> text box to indicate the data stored in this field (a single row editing box)


Xmlfield

Whether the verification value is a textfield of legal XML. The parameter schema_path must be provided. It is a file system path used to verify the relaxng schema of text.

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.