Django model type

Source: Internet
Author: User

Django uses models to create, modify, and delete databases. This document describes a list of common types in the model for ease of query and use:

Autofield: An auto-incrementing integer field 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: Boolean field, which is automatically described as checkbox in the management tool. Charfield: string field, single line input, used for short strings. To save a large amount of text, use textfield. charfield has a required parameter:

Charfield. max_length: Maximum length of a character. Django limits the maximum number of characters allowed by this field at the database layer and validation layer based on this parameter.

Textfield: A large text field. The admin management interface uses the <textarea> multi-row edit box to represent the field data. Commaseparatedintegerfield: used to store comma-separated integer values. Similar to charfield, The maxlength parameter is required. Datefield: Date Field. Admin uses a text box <input type = "text"> to represent the field data (with a javascript calendar and a "today" shortcut key. There are the following additional optional parameters:

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.

Datetimefield: similar to datefield, it supports the same additional options. Emailfield: A charfield with email validity check. The maxlength parameter is not accepted. 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 ). To use filefield or imagefield in a model, follow these steps: 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. 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. 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 in the template. Get_mug_shot_url} to obtain the absolute path of the image. Filepathfield: select a specified directory and select a file according to the restriction rule. Three parameters are optional, and "path" is required. These three parameters can be used at the same time. parameter description:

Path: required parameter, the absolute file system path of a directory. Filepathfield to obtain the optional items. Example: "/home/images ";
Match: an optional parameter. It is a regular expression. As a string, filepathfield uses it to filter file names. Note that this regular expression applies only 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. whether to include all subdirectories under path, which are true or false. The default value is false.

Match only applies to base filename, not the full name of the path. For example, filepathfield (Path = "/home/images", match = "Foo. *", recursive = true )... /Home/images/foo.gif is matched, but/home/images/Foo/bar.gif is not matched.

Floatfield: Float field. Two parameters must be provided. parameter description:

Max_digits: Total digits (excluding decimal points and symbols)
Decimal_places: number of decimal places. For example, to save the maximum value of 999 (2 digits after the decimal point), you need to define the field: models. floatfield (..., Max_digits = 5, decimal_places = 2). To save the maximum value of 1 million (10 digits after the decimal point), define models. floatfield (..., Max_digits = 19, decimal_places = 10)

Imagefield: similar to filefield, but check 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 the python imaging library. Integerfield: used to save an integer. Ipaddressfield: an IP address in string format (for example, "202.1241.30 ″). 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 with valid American-style phone number verification in the format of XXX-XXX-XXXX ). Positiveintegerfield: similar to integerfield, but the value range is non-negative integer (This field should allow 0 values... It can be understood as an unsigned integer) positivesmallintegerfield:

Positive and small integer fields, similar to positiveintegerfield, have a small value range (Database-related). slugfield "slug" is a newspaper term. Slug is a small sign (short sign) for something. It only contains letters, numbers, underscores, and hyphens. They are usually used for URLs. If you use the Django development version, you can specify maxlength. If maxlength is not specified, Django uses the default length: 50, which accepts an additional parameter:

Prepopulate_from: auto-preset list from slug

Slugfield: 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. Smallintegerfield: similar to integerfield, but only integers within a certain value range are allowed. (Dependent on the database) timefield: time field, similar to datefield and datetimefield. Urlfield: used to save the URL. If the verify_exists parameter is true (default), the given URL is checked in advance (that is, whether the URL is effectively loaded and no 404 response is returned ). Usstatefield: the abbreviation of the U.S. state. It consists of two letters (which are ignored by the people of tianchao ). Xmlfield: XML character field. If the check value is a valid XML textfield, you must provide the following parameters:

Schema_path: Verify the file system path of the relaxng schema of the text.

Appendix: field options
    • Null: The default value is false. It is usually not used for character fields, such as charfield and textfield. If no value exists, an empty string is returned.

    • Blank: whether the field can be empty. If it is false, a value is required.

    • Choices: a two-dimensional metagroup used to select values. The first value is the actually stored value, and the second value is used for selection. For example, sex_choices = ('F', 'female '), ('M', 'male '),)

    • Core: db_column. If db_index is true, an index will be created for this field.

    • Default: Set the default value.

    • Editable: If it is false, it cannot be rewritten in admin mode. True by default

    • Help_text: Help document in admin mode

    • Primary_key: Set the primary key. If the primary key is not set, it will automatically

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.