大家好,我是Leo
今天要來講解,如何讓dbName作為變數在ORM上面使用
OK~~~ Let's go now!!!
models/db_variable_model.py
'''
分別有Variable1,Variable2兩個table,如果今天我們有重複的dbtable,如何做到一個fun呼叫多個table
我們可以發現兩個table的col都一樣,不同點是tableName 1與2的差別
於是我們可以定義一個getModel的fun
class model 放置需要讀取的欄位,最後再將dbtable當作變數使用,並回傳table
managed = False 指的是不生成此名稱資料表在db內
'''
from django.db import models
class Variable1(models.Model):
id = models.AutoField(primary_key=True)
sex = models.IntegerField(null=True,blank=True)
age = models.IntegerField(null=True,blank=True)
class Variable2(models.Model):
id = models.AutoField(primary_key=True)
sex = models.IntegerField(null=True,blank=True)
age = models.IntegerField(null=True,blank=True)
def getModel(dbtable):
class Variable(models.Model):
id = models.AutoField(primary_key=True)
sex = models.IntegerField(null=True, blank=True)
age = models.IntegerField(null=True, blank=True)
class Meta:
db_table = dbtable
managed = False
return Variable
models/init.py
from .db_variable_model import *
makemigrations/0004_add_variable.py
這邊使用自己makemigrations內的最後一筆num+1
example我這邊最後一筆0003,於是我創建0004_add_variable.py
from __future__ import unicode_literals
from django.db import models, migrations
def add_variable(apps, schema_editor):
# apps.get_model(app,table)
variable1_datas = apps.get_model("website", "Variable1")
variable1_data1 = variable1_datas(id=1, sex=1, age=22)
variable1_data1.save()
variable1_data2 = variable1_datas(id=2, sex=2, age=23)
variable1_data2.save()
variable1_data3 = variable1_datas(id=3, sex=1, age=25)
variable1_data3.save()
variable1_data4 = variable1_datas(id=4, sex=1, age=27)
variable1_data4.save()
variable1_data5 = variable1_datas(id=5, sex=2, age=28)
variable1_data5.save()
variable2_datas = apps.get_model("website", "Variable2")
variable2_data1 = variable2_datas(id=1, sex=2, age=33)
variable2_data1.save()
variable2_data2 = variable2_datas(id=2, sex=2, age=35)
variable2_data2.save()
variable2_data3 = variable2_datas(id=3, sex=2, age=37)
variable2_data3.save()
def delete_all(apps, schema_editor):
return 'SET FOREIGN_KEY_CHECKS=0; DELETE from website_variable1; DELETE from website_variable2;'
class Migration(migrations.Migration):
# 記得這裡要改成上一張表的名稱
dependencies = [
('website', '0003_hot_pot_store'),
]
operations = [
migrations.RunPython(add_variable,delete_all),
]
設定好後在Terminal執行以下
# add 2 tables about variable1,2
# 將假資料寫入 variable1,2內
python .\manage.py makemigrations
python .\manage.py migrate
views/db_variable.py
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework import generics,permissions
from website.models.db_variable_model import getModel
class VariableAPIView(APIView):
permission_classes = (permissions.AllowAny,)
authentication_classes = []
def post(self, request):
db_table = 'website_variable'
result = list()
for i in range(1,3):
# 取到 website_variable1,website_variable2
db_table_name = db_table + str(i)
# 執行function 將model資料回傳
variable = getModel(db_table_name)
# 取到objects內的資訊,這邊如果資料表在不同db也可以使用.objects.using(db).all()
#.values 轉換成dict的型式
variable_data = (
variable.objects.all().values('id','sex','age')
)
for data in variable_data:
result.append(data)
return Response(result)
website_variable1
website_variable2
viewsData
我們已將variable1,variable2 table內的資料取出並合併
今天主要是介紹當很多情況下,我們使用的table如果要當成變數使用的話,我們該如何製作,在很多情況下我們db可能不只一個,連帶tableName內也有可能是create by date,當面對這種巨大的table時我們不可能每個table都寫一個,希望透過一個fun來達成呼叫所有table,那我們明天預計介紹如何佈署到gcp上面,以及使用無伺服器應用程式託管平臺appengine的方式。
我們明天見,各位掰掰~~~