今天来说说web框架Django怎么配置使用数据库,也就是传说中MVC(Model View Controller)中的M,Model(模型)。

简单介绍一下Django中的MVC:

模型(model):定义你的数据库,一般都在models.py文件中。

视图(view):定义你的HTML等静态网页文件相关,也就是那些html、css、js等前端的东西。

控制器(controller):定义你的业务逻辑相关,就是你的主要代码。

正文开始

首先要在你的Django项目中找到两个文件:setting.py、models.py

然后保证你的app要注册到setting里面哟

找到DATABASES处,该处就是配置数据库的地方,然后进行设置:

首先如果是才创建的项目,那么你的DATABASES应该是长这样的:

Django源生默认使用的自带数据库sqlite3。如果你要使用自带的,那你可以完全不用动这里的设置,如果你要使用其他的数据库,更改ENGINE的值即可,比如mysql:django.db.backends.mysql

当我们使用其他数据库时,必须对其他参数进行设置,不然Django可能连不上你指定的数据库,我这里拿mysql来举例

可以看到,多了一些参数,NAME就是你要使用的数据库名字(实现要创建好,不然Django找不到),USER就是数据库登陆账号,然后依次是密码,IP,端口。

到这里setting应该就算是配置完成了,接着打开models.py,进行编辑

最后通过CMD进入到你的Django目录,执行python manage.py makemigrations

再执行python manage.py migrate

成功ok!撒花

如果要进行多数据库配置,研究了一番,终于搞通了,确实要复杂一些。不过也不是很复杂,多了两三个步骤,下面我们一个一个步骤来

首先只需要在DATABASE下继续添加一个字典即可,例如我添加了一个yq,里面写清楚了我这个yq要引用的数据库

然后就是添加路由器DATABASE_ROUTERS和映射DATABASE_APPS_MAPPING

看到了DATABASE_ROUTERS = ['Yq_Djago.database_router.DatabaseAppsRouter']吗,这个就是路由器的地址,意思是用Yq_Djago项目中的database_router文件里面的DatabaseAppsRouter方法

所以我们现在要去这个路径下创建一个database_router.py文件,然后进行如下编辑

from django.conf import settings

DATABASE_MAPPING = settings.DATABASE_APPS_MAPPING

class DatabaseAppsRouter(object):
"""
A router to control all database operations on models for different
databases. In case an app is not set in settings.DATABASE_APPS_MAPPING, the router
will fallback to the `default` database. Settings example: DATABASE_APPS_MAPPING = {'app1': 'db1', 'app2': 'db2'}
""" def db_for_read(self, model, **hints):
""""Point all read operations to the specific database."""
if model._meta.app_label in DATABASE_MAPPING:
return DATABASE_MAPPING[model._meta.app_label]
return None def db_for_write(self, model, **hints):
"""Point all write operations to the specific database."""
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."""
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.
"""
if db in DATABASE_MAPPING.values():
return DATABASE_MAPPING.get(app_label) == db
elif app_label in DATABASE_MAPPING:
return False
return None

最后再在models.py里面指定我们的表运用的是哪个数据,继续拿我自己写的例子举例

最后通过CMD进入到你的Django目录,执行python manage.py makemigrations

然后再创建数据库yq,执行python manage.py migrate --database=yq(不如不写--database就是默认创建default里面的)

打开数据库查看,大功告成!创建的interface和interface2两张表都在里面,撒花!!!

最新文章

  1. SQL.WITH AS.公用表表达式(CTE)(转)
  2. liunx之:top命令解释
  3. ElasticSearch Filter Aggregations
  4. apt-get -y install中的-y是什么意思?
  5. API 双方认证探讨
  6. php中global和$GLOBALS[]的分析之一
  7. JS子元素oumouseover触发父元素onmouseout
  8. ntopng汉化记录
  9. JAVA面向对象-----抽象类
  10. Leetcode 88. Merge Sorted Array(easy)
  11. Eclipse Tomcat部署web项目时出现There are no resources that can be added or removed from the server解决办法
  12. pytorch构建自己的数据集
  13. VSTO:使用C#开发Excel、Word【17】
  14. cocoa pods自己的笔记
  15. zombodb sql functions 说明
  16. NodeJS学习:搭建私有NPM
  17. tensorflow实战系列(二)TFRecordReader
  18. 稳聘App设计图分享
  19. HDU 2894 DeBruijin (数位欧拉)
  20. 论文笔记:目标检测算法(R-CNN,Fast R-CNN,Faster R-CNN,FPN,YOLOv1-v3)

热门文章

  1. Spark Streaming性能优化系列-怎样获得和持续使用足够的集群计算资源?
  2. Nova镜像使用方法
  3. 下载Google官方/CM Android源码自己主动又一次開始的Shell脚本
  4. win10安装Anaconda+TensorFlow+配置PyCharm
  5. HTML5 and Websocket
  6. babel的安装和使用方法
  7. 对于iOS 7 隐藏特性和解决之道
  8. adb获取Android系统属性(adb shell getprop ***)数据来源
  9. !important的用法(IE6 兼容的解决方法)
  10. linux内核的三种主要调度策略