Django dynamic type operation utilizing the database table

Scene analysis:

MySql backend database holds a large number of press ticker named data tables, each table is stored in each stock's daily data.

stock_000002

stock_600030

stock_600020

... a total of more than 3,000 tables.

Django. If you want the ticker to show the data of each table, using conventional ORM model becomes very difficult, because there are more than 3,000 tables, we need to build more than 3,000 models.

 

Solution:

1. The table names, and other fields to create a dynamic table according to the ORM model

def create_model(name, fields=None, app_label='', module='', options=None, admin=None):
    class Meta:
        pass
    if app_label:
        setattr(Meta, 'app_label', app_label)
    if options is not None:
        for key, value in options.items():
            setattr(Meta, key, value)
    attrs = {'__module__': module, 'Meta': Meta}
    if fields:
        attrs.update(fields)
    # 继承models.Model
    return type(name, (models.Model,), attrs)

 

2. Call create_model, returned custom_model ORM model is corresponding to the table

DEF new_stock (TAB_NAME):
     "" " 
    dynamic data model is created 
    : param tab_name: Table Name 
    : return: Returns the model class 
    " "" 
    Fields = {
         ' ts_code ' : models.CharField (= 20 is MAX_LENGTH ),
         ' trade_date ' : Models. as CharField (= 20 is MAX_LENGTH, UNIQUE = True),
         ' Open ' : models.FloatField (null = False, default = 0.0 ),
         ' High ' : models.FloatField (null = False, default = 0.0 ),
         ' Low ' : Models .FloatField (null = False,default=0.0),
        'close': models.FloatField(null=False, default=0.0),
        'pre_close': models.FloatField(null=False, default=0.0),
        'change': models.FloatField(null=False, default=0.0),
        'pct_chg': models.FloatField(null=False, default=0.0),
        'vol': models.FloatField(null=False, default=0.0),
        'amount': models.FloatField(null=False, default=0.0)
    }
    options = {'ordering': ['trade_date'], 'db_table': tab_name, }
    custom_model = create_model(tab_name, fields, options=options, app_label='stock', module='stock.models')
    return custom_model

 

3. http request daily stock data by date in descending order.

DEF stock_detail (Request, PK): 
    TAB_NAME = ' stock_ ' + PK 
    stock_mod = new_stock (TAB_NAME) 

    # query 
    IF request.method == ' the GET ' : 
        limit = request.GET.get ( ' limit ' ) 
        offset = `` request.GET`` .get ( ' offset ' )
         # query the total number of records 
        total = stock_mod.objects.count () 

        # result in descending order by date 
        DataList = stock_mod.objects.all (). ORDER_BY ( '-trade_date')[int(offset): int(offset)+int(limit)]
        json_list = []

        import json
        # 返回json字符串
        for data in datalist:
            json_dict = model_to_dict(data)
            json_list.append(json_dict)

        result = dict()
        rows = list()
        result['total'] = total
        result['rows'] = json_list

        logger.info('Get the stock list ' )
         return jsonResponse (the Result, Safe = False)

HTTP request: GET / stock / 000002 / offset = 0 & limit = 10?

000002 with "stock_" combined into a table "stock_000002"

Data offset is the start position

to limit the number of return data

datalist data obtained by table lookup organized into json format back to the browser.

 

Guess you like

Origin www.cnblogs.com/bryant24/p/11445688.html