成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久

您的位置:首頁技術(shù)文章
文章詳情頁

Django多數(shù)據(jù)庫配置及逆向生成model教程

瀏覽:8日期:2024-10-18 10:46:10

在項(xiàng)目中我們每個app對應(yīng)不同的數(shù)據(jù)庫,其中有一個是從數(shù)據(jù)庫逆向生成model,做個筆記。

1、修改項(xiàng)目的setting.py配置 :

DATABASES = { ’default’: { ’ENGINE’: ’django.db.backends.mysql’, # 默認(rèn)用mysql ’NAME’: ’bk’, # 數(shù)據(jù)庫名 (默認(rèn)與APP_ID相同) ’USER’: ’root’, # 你的數(shù)據(jù)庫user ’PASSWORD’: ’root’, # 你的數(shù)據(jù)庫password ’HOST’: ’xxx.xxx.xxx.xxx’, # 開發(fā)的時候,使用localhost ’PORT’: ’3306’, # 默認(rèn)3306 }, ’cloudsino_test’: { ’ENGINE’: ’django.db.backends.mysql’, # 默認(rèn)用mysql ’NAME’: ’cloudsino_test’, # 數(shù)據(jù)庫名 (默認(rèn)與APP_ID相同) ’USER’: ’root’, # 你的數(shù)據(jù)庫user ’PASSWORD’: ’root’, # 你的數(shù)據(jù)庫password ’HOST’: ’xxx.xxx.xxx.xxx’, # 開發(fā)的時候,使用localhost ’PORT’: ’3306’, # 默認(rèn)3306 },}# 設(shè)置數(shù)據(jù)庫的路由規(guī)則方法DATABASE_ROUTERS = [’conf.database_router.DatabaseAppsRouter’]# 設(shè)置APP對應(yīng)的數(shù)據(jù)庫路由表,哪個app要連接哪個數(shù)據(jù)庫,沒有指定會用default那個。DATABASE_APPS_MAPPING = { # example: #’app_name’:’database_name’, ’home_application’: ’cloudsino_test’, ’cmdb’: ’default’,}

2、新建database_router.py:

在與setting.py文件同級的目錄下新建database_router.py文件:

# -*- coding: utf-8 -*-from settings_development import DATABASE_APPS_MAPPINGDATABASE_MAPPING = DATABASE_APPS_MAPPINGclass DatabaseAppsRouter(object): def db_for_read(self, model, **hints): ''''建議model類型對象從哪一個數(shù)據(jù)庫讀取.''' if model._meta.app_label in DATABASE_MAPPING: return DATABASE_MAPPING[model._meta.app_label] return None def db_for_write(self, model, **hints): '''建議model類型對象的寫入操作應(yīng)該使用哪個數(shù)據(jù)庫''' if model._meta.app_label in DATABASE_MAPPING: return DATABASE_MAPPING[model._meta.app_label] return None def allow_relation(self, obj1, obj2, **hints): '''Allow any relation between apps that use the same database. 如果obj1 和obj2 之間應(yīng)該允許關(guān)聯(lián)則返回True,如果應(yīng)該防止關(guān)聯(lián)則返回False,如果路由無法判斷則返回None ''' db_obj1 = DATABASE_MAPPING.get(obj1._meta.app_label) db_obj2 = DATABASE_MAPPING.get(obj2._meta.app_label) if db_obj1 and db_obj2: if db_obj1 == db_obj2: return True else: return False return None def allow_syncdb(self, db, model): '''Make sure that apps only appear in the related database.''' if db in DATABASE_MAPPING.values(): return DATABASE_MAPPING.get(model._meta.app_label) == db elif model._meta.app_label in DATABASE_MAPPING: return False return None def allow_migrate(self, db, app_label, model=None, **hints): ''' Make sure the auth app only appears in the ’auth_db’ database. 定義遷移操作是否允許在別名為db的數(shù)據(jù)庫上運(yùn)行。如果操作應(yīng)該運(yùn)行則返回True ,如果不應(yīng)該運(yùn)行則返回False,如果路由無法判斷則返回None。 ''' if db in DATABASE_MAPPING.values(): return DATABASE_MAPPING.get(app_label) == db elif app_label in DATABASE_MAPPING: return False return None

3.逆向生成model:

在terminal中輸入以下命令,使用名為cloudsino_test的DATABASE來逆向生成model到home_application這個app的models.py:

python manage.py inspectdb --database=cloudsino_test > home_application/models.py

Django多數(shù)據(jù)庫配置及逆向生成model教程

逆向生成的model:

# This is an auto-generated Django model module.# You’ll have to do the following manually to clean this up:# * Rearrange models’ order# * Make sure each model has one field with primary_key=True# * Remove `managed = False` lines if you wish to allow Django to create, modify, and delete the table# Feel free to rename the models, but don’t rename db_table values or field names.## Also note: You’ll have to insert the output of ’django-admin sqlcustom [app_label]’# into your database.from __future__ import unicode_literalsfrom django.db import modelsclass CloudsinoCpuinfo(models.Model): corenumber = models.CharField(max_length=128) frequency = models.CharField(max_length=128) index = models.CharField(max_length=128) manufacturer = models.CharField(max_length=128) name = models.CharField(max_length=128) partnumber = models.CharField(max_length=128) serialnumber = models.CharField(max_length=128) type = models.CharField(max_length=128) device = models.ForeignKey(’CloudsinoDevice’, blank=True, null=True) class Meta: managed = False db_table = ’cloudsino_cpuinfo’class CloudsinoDevice(models.Model): sn = models.CharField(max_length=128) app = models.CharField(max_length=128) business_chargeby1 = models.CharField(max_length=128) business_chargeby2 = models.CharField(max_length=128) chargeby1 = models.CharField(max_length=128) chargeby2 = models.CharField(max_length=128) description = models.CharField(max_length=128) device_status = models.CharField(max_length=128) devicename = models.CharField(max_length=128) field1 = models.CharField(max_length=128) field2 = models.CharField(max_length=128) field3 = models.CharField(max_length=128) field4 = models.CharField(max_length=128) field5 = models.CharField(max_length=128) frame_posiniton = models.CharField(max_length=128) framename = models.CharField(max_length=128) ip = models.CharField(max_length=128) manufacturer = models.CharField(max_length=128) model = models.CharField(max_length=128) os = models.CharField(max_length=128) position_desc = models.CharField(max_length=128) roomarea = models.CharField(max_length=128) roomname = models.CharField(max_length=128) servicetag = models.CharField(max_length=128) shape = models.CharField(max_length=128) specification = models.CharField(max_length=128) subtype = models.CharField(max_length=128) type = models.CharField(max_length=128) ucount = models.CharField(max_length=128) class Meta: managed = False db_table = ’cloudsino_device’class CloudsinoDevicetype(models.Model): name = models.CharField(max_length=128) type = models.CharField(max_length=128) status = models.CharField(max_length=128) class Meta: managed = False db_table = ’cloudsino_devicetype’class CloudsinoDiskinfo(models.Model): bus = models.CharField(max_length=128) formfactor = models.CharField(max_length=128) index = models.CharField(max_length=128) manufacturer = models.CharField(max_length=128) media = models.CharField(max_length=128) name = models.CharField(max_length=128) partnumber = models.CharField(max_length=128) serialnumber = models.CharField(max_length=128) size = models.CharField(max_length=128) speed = models.CharField(max_length=128) type = models.CharField(max_length=128) device = models.ForeignKey(CloudsinoDevice, blank=True, null=True) class Meta: managed = False db_table = ’cloudsino_diskinfo’class CloudsinoFaninfo(models.Model): index = models.CharField(max_length=128) name = models.CharField(max_length=128) partnumber = models.CharField(max_length=128) serialnumber = models.CharField(max_length=128) device = models.ForeignKey(CloudsinoDevice, blank=True, null=True) class Meta: managed = False db_table = ’cloudsino_faninfo’class CloudsinoHbacardinfo(models.Model): name = models.CharField(max_length=128) wwnn = models.CharField(max_length=128) wwpn = models.CharField(max_length=128) fc_switch = models.CharField(max_length=128) switch_port = models.CharField(max_length=128) switch_mac = models.CharField(max_length=128) device = models.ForeignKey(CloudsinoDevice, blank=True, null=True) class Meta: managed = False db_table = ’cloudsino_hbacardinfo’class CloudsinoManageinfo(models.Model): assetcode = models.CharField(max_length=128) department = models.CharField(max_length=128) express_code = models.CharField(max_length=128) service_level = models.CharField(max_length=128) shutdown_level = models.CharField(max_length=128) usage = models.CharField(max_length=128) device = models.ForeignKey(CloudsinoDevice, blank=True, null=True) class Meta: managed = False db_table = ’cloudsino_manageinfo’class CloudsinoManufacturertype(models.Model): name = models.CharField(max_length=128) type = models.CharField(max_length=128) status = models.CharField(max_length=128) class Meta: managed = False db_table = ’cloudsino_manufacturertype’class CloudsinoMemory(models.Model): name = models.CharField(max_length=128) type = models.CharField(max_length=128) frequency = models.CharField(max_length=128) size = models.CharField(max_length=128) manufacturer = models.CharField(max_length=128) partnumber = models.CharField(max_length=128) serialnumber = models.CharField(max_length=128) index = models.CharField(max_length=128) memoryinfo = models.ForeignKey(’CloudsinoMemoryinfo’, blank=True, null=True) class Meta: managed = False db_table = ’cloudsino_memory’class CloudsinoMemoryinfo(models.Model): max_dimmslots = models.CharField(max_length=128) mem_totalsize = models.CharField(max_length=128) memmax_capacitysize = models.CharField(max_length=128) populated_dimmslots = models.CharField(max_length=128) device = models.ForeignKey(CloudsinoDevice, blank=True, null=True) class Meta: managed = False db_table = ’cloudsino_memoryinfo’class CloudsinoNetworkinfo(models.Model): name = models.CharField(max_length=128) type = models.CharField(max_length=128) mac = models.CharField(max_length=128) speed = models.CharField(max_length=128) manufacturer = models.CharField(max_length=128) partnumber = models.CharField(max_length=128) serialnumber = models.CharField(max_length=128) index = models.CharField(max_length=128) device = models.ForeignKey(CloudsinoDevice, blank=True, null=True) class Meta: managed = False db_table = ’cloudsino_networkinfo’class CloudsinoOobnetwork(models.Model): ip = models.CharField(max_length=128) netmask = models.CharField(max_length=128) gateway = models.CharField(max_length=128) mac = models.CharField(max_length=128) ethernet_switch = models.CharField(max_length=128) swith_port = models.CharField(max_length=128) swith_mac = models.CharField(max_length=128) distribution_frame = models.CharField(max_length=128) distribution = models.CharField(max_length=128) device = models.ForeignKey(CloudsinoDevice, blank=True, null=True) class Meta: managed = False db_table = ’cloudsino_oobnetwork’class CloudsinoPciecard(models.Model): name = models.CharField(max_length=128) type = models.CharField(max_length=128) loc = models.CharField(max_length=128) online_state = models.CharField(max_length=128) conntype = models.CharField(max_length=128) bandwidth = models.CharField(max_length=128) speed = models.CharField(max_length=128) manufacturer = models.CharField(max_length=128) partnumber = models.CharField(max_length=128) serialnumber = models.CharField(max_length=128) index = models.CharField(max_length=128) device = models.ForeignKey(CloudsinoDevice, blank=True, null=True) class Meta: managed = False db_table = ’cloudsino_pciecard’class CloudsinoPowerinfo(models.Model): name = models.CharField(max_length=128) type = models.CharField(max_length=128) model = models.CharField(max_length=128) outputpower = models.CharField(max_length=128) manufacturer = models.CharField(max_length=128) partnumber = models.CharField(max_length=128) serialnumber = models.CharField(max_length=128) index = models.CharField(max_length=128) device = models.ForeignKey(CloudsinoDevice, blank=True, null=True) class Meta: managed = False db_table = ’cloudsino_powerinfo’class CloudsinoProductnetwork(models.Model): ip = models.CharField(max_length=128) netmask = models.CharField(max_length=128) gateway = models.CharField(max_length=128) mac = models.CharField(max_length=128) os = models.CharField(max_length=128) remote_type = models.CharField(max_length=128) remote_port = models.CharField(max_length=128) ethernet_switch = models.CharField(max_length=128) swith_port = models.CharField(max_length=128) swith_mac = models.CharField(max_length=128) distribution_frame = models.CharField(max_length=128) distribution = models.CharField(max_length=128) device = models.ForeignKey(CloudsinoDevice, blank=True, null=True) class Meta: managed = False db_table = ’cloudsino_productnetwork’class CloudsinoPurchasewarrantyinfo(models.Model): date_manuf = models.CharField(max_length=128) expiredate = models.CharField(max_length=128) price = models.CharField(max_length=128) purchase_order = models.CharField(max_length=128) purchase_order_name = models.CharField(max_length=128) purchase_supply = models.CharField(max_length=128) serviceagent = models.CharField(max_length=128) warrantyitem = models.CharField(db_column=’warrantyItem’, max_length=128) # Field name made lowercase. warrantyperiod = models.CharField(db_column=’warrantyPeriod’, max_length=128) # Field name made lowercase. warrantystartdate = models.CharField(db_column=’warrantyStartDate’, max_length=128) # Field name made lowercase. warrantytype = models.CharField(db_column=’warrantyType’, max_length=128) # Field name made lowercase. device = models.ForeignKey(CloudsinoDevice, blank=True, null=True) class Meta: managed = False db_table = ’cloudsino_purchasewarrantyinfo’class CloudsinoRaidinfo(models.Model): name = models.CharField(max_length=128) type = models.CharField(max_length=128) cachesize = models.CharField(max_length=128) speed = models.CharField(max_length=128) manufacturer = models.CharField(max_length=128) partnumber = models.CharField(max_length=128) serialnumber = models.CharField(max_length=128) index = models.CharField(max_length=128) device = models.ForeignKey(CloudsinoDevice, blank=True, null=True) class Meta: managed = False db_table = ’cloudsino_raidinfo’class DjangoMigrations(models.Model): app = models.CharField(max_length=255) name = models.CharField(max_length=255) applied = models.DateTimeField() class Meta: managed = False db_table = ’django_migrations’

補(bǔ)充知識:Django使用數(shù)據(jù)庫生成模型類

正常的開發(fā)流程

在models.py中定義模型類,要求繼承自models.Model

把應(yīng)用加入settings.py文件的installed_app項(xiàng)

生成遷移文件

執(zhí)行遷移生成表

使用模型類進(jìn)行crud操作

使用數(shù)據(jù)庫生成模型類

python manage.py inspectdb > app_name/models.py

例如:

python manage.py inspectdb > booktest/models.py

以上這篇Django多數(shù)據(jù)庫配置及逆向生成model教程就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Django
相關(guān)文章:
成人在线亚洲_国产日韩视频一区二区三区_久久久国产精品_99国内精品久久久久久久
一区二区日韩免费看| 久久99久久精品| 午夜精品亚洲| 青草av.久久免费一区| 欧美国产一区视频在线观看| 91国产成人在线| 亚洲婷婷在线| 国产麻豆午夜三级精品| 午夜av区久久| 国产精品对白交换视频| 91麻豆精品国产自产在线观看一区 | 精品国产一区二区三区忘忧草 | 国产麻豆欧美日韩一区| 欧美一级在线免费| 亚洲国内精品| 丁香桃色午夜亚洲一区二区三区| 国产精品久久久久久久久动漫| 日韩欧美一卡二卡| 欧美日韩一区小说| 亚洲欧美日韩在线观看a三区| 极品少妇一区二区三区| 欧美1区3d| 成人久久18免费网站麻豆| 日韩影院免费视频| 亚洲一区二区高清| 亚洲综合999| 一区二区成人在线| 国产精品丝袜91| 久久久久久久久久久久电影| 宅男在线国产精品| 欧美日韩亚洲综合在线 欧美亚洲特黄一级 | 日韩视频免费观看高清完整版在线观看| 久久久精品动漫| 亚洲一级在线| 亚洲在线播放电影| 亚洲一级在线| 国产精品尤物| 国产一区二区你懂的| 国产欧美日韩在线播放| 国产精品美女诱惑| 久久久久久亚洲精品不卡4k岛国| 午夜精品久久久久久| 亚洲国产aⅴ成人精品无吗| 亚洲男人天堂av| 一区在线观看视频| 亚洲男人的天堂在线aⅴ视频| 亚洲激情校园春色| 性感美女久久精品| 视频一区视频二区中文| 久久99最新地址| 精久久久久久久久久久| 成人性生交大合| 欧美福利精品| 国产精品亚洲不卡a| 美女黄网久久| 欧美伊人精品成人久久综合97| 欧美午夜一区二区三区| 欧美一级久久久| 国产精品久久网站| 亚洲高清一区二区三区| 久久精品国产99| 国产一区 二区| 欧美日韩亚洲一区二区三区在线观看 | 日韩欧美国产午夜精品| 国产精品卡一卡二卡三| 石原莉奈在线亚洲二区| 国产不卡视频一区| 欧美午夜电影在线观看| 久久久www| 亚洲精品一区二区三区在线观看| 国产精品女上位| 婷婷开心久久网| 国产一区二区三区久久久| 91浏览器在线视频| 色噜噜久久综合| 久久久五月婷婷| 亚洲午夜久久久| 成人午夜视频免费看| 亚洲一区二区三区精品在线观看 | 欧美黄色一区| 色哟哟在线观看一区二区三区| 精品国产一区a| 亚洲大片精品永久免费| 成人精品国产免费网站| 亚洲免费网址| 日韩免费一区二区| 午夜精品免费在线观看| 91老师片黄在线观看| 欧美色图一区二区三区| 成人欧美一区二区三区视频网页| 国产美女主播视频一区| 国产精品久久九九| 国产偷国产偷精品高清尤物| 麻豆一区二区在线| 99www免费人成精品| 精品成人一区二区| 免费看日韩精品| 99国产精品视频免费观看一公开| 精品美女被调教视频大全网站| 婷婷综合五月天| 激情综合亚洲| 久久久久国产成人精品亚洲午夜| 久久99精品一区二区三区三区| 99国产成+人+综合+亚洲欧美| 久久久久久99精品| 国产精品一区在线观看乱码| 一本色道**综合亚洲精品蜜桃冫| 日韩美女精品在线| 欧美一区亚洲| 久久嫩草精品久久久精品| 国产伦精品一区二区三区免费迷 | 在线综合+亚洲+欧美中文字幕| 五月激情综合色| 一区二区久久| 亚洲激情综合网| 一区二区精品| 亚洲视频图片小说| 亚洲伦伦在线| 亚洲精品老司机| 在线综合视频| 亚洲不卡av一区二区三区| 久久人人九九| 亚洲午夜精品17c| 性刺激综合网| 午夜视频一区二区三区| 亚洲精品一二| 亚洲三级久久久| 黄色在线成人| 亚洲视频一二三| 一区二区三区福利| 亚洲一区视频在线| 久久精品电影| 美女mm1313爽爽久久久蜜臀| 在线电影国产精品| 国产91在线观看| 久久久久国产精品人| 91久久亚洲| 亚洲.国产.中文慕字在线| 色婷婷一区二区三区四区| 狠狠久久亚洲欧美| 久久先锋影音av| 91久久精品国产91久久性色tv| 一区二区在线观看视频| 久久五月天婷婷| 国产风韵犹存在线视精品| 久久久五月婷婷| 亚洲欧美日韩视频二区| 五月婷婷综合网| 在线观看亚洲精品| 国产精品一区不卡| 国产女主播一区| 1024亚洲| 天天色综合成人网| 一本色道久久综合亚洲aⅴ蜜桃| 婷婷久久综合九色综合伊人色| 蜜桃av综合| 久久精品国产在热久久| 日韩一级精品| 日韩激情av在线| 91精品国产一区二区三区蜜臀| 成人国产精品免费观看| 久久综合给合久久狠狠狠97色69| 欧美午夜一区二区福利视频| 综合色中文字幕| 欧美在线三区| 亚洲精品免费电影| 在线观看日韩精品| 成人午夜激情在线| 久久久久久久性| 国产精品久久久一区二区| 首页欧美精品中文字幕| 欧美四级电影在线观看| 99久久久精品免费观看国产蜜| 国产精品国产a级| 久久国产手机看片| 国产99久久久国产精品免费看| 国产精品久久久久毛片软件| 久久久精品动漫| 麻豆91在线观看| 欧美国产一区二区在线观看| 一区在线视频| 国产一区二三区好的| 久久久久国产精品麻豆| 亚洲男人影院| 成人蜜臀av电影| 日韩成人午夜精品| 中文欧美字幕免费| 国产精品香蕉一区二区三区| 亚洲女性喷水在线观看一区| 欧美美女黄视频| 在线视频观看日韩| 99久免费精品视频在线观看| 亚洲成av人片在线| 欧美啪啪一区| 国产精品资源站在线| 亚洲图片欧美色图| 精品美女一区二区| 国产精品日韩一区二区| av成人免费在线| 美女国产一区二区|