"Django Models" virtualization extracts Models public functionality

Source: Internet
Author: User

1. Demand

The Django models defines the tables of the database, and many of the tables in the process must have many common operations. Then you can encapsulate some of the common operations, and you don't need to repeat the definition and use in each table.

2. Definition

or inherit from models, define a Jsonmode base class

Class Jsonmode (models. Model):

2.1 Virtualization

#Model基类, you're not going to create a data table
#默认按照时间逆序排列
Class Meta:
abstract = True

2.2 Public fields, such as sort

#Model基类, you're not going to create a data table
#默认按照时间逆序排列
Class Meta:
abstract = True
ordering = ['-modifytime ', '-createtime ', '-id ']

3. Public functions

For example, some of the functions are selected, and this function has normal argument self and other parameters

3.1 Field operations that are not yet defined

It is particularly important to note that, depending on the column name, the following actions are generally required to models the data fields.

Self.name = ' This is a test '

In Jsonmode, ColName is passed in by parameters, you can use the properties of the Python object to set functions and get functions setattr and GetAttr

#根据列名, the old value is retained, the new value is temporarily placed at the end, and the callback is refreshed after the file is successfully uploaded
SetAttr (Self, colname,getattr (Self,colname, ") + Globalvar.interstr + filename[0])

Method Invocation of 3.2 models

Models method call, directly using the self call, such as Self.save ()


4, sub-class implementation

For example, Filtetemp inherits Jsonmode and calls the parent class's function using Super (FILETEMP,SERL). function can be

"Django Models" virtualization extracts Models public functionality

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.