在django中为每个模型设置db [英] set db per model in django

查看:166
本文介绍了在django中为每个模型设置db的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在查看django的多db文档。我想把我的几个模型分解成不同的数据库。但我真的只是希望这些模型始终都在一个特定的数据库中。我不需要特殊路由。并且编写唯一的路由器只是说模型A,B和C住在数据库X中,模型D,E和F总是存在于数据库Y。

I've been looking over django's multi-db docs. I'd like to break a few of my models out into a different db. But I really just want those models to ALWAYS live in a particular db. I don't need special routing. And writing unique routers just to say "Models A, B, and C live in database X, models D, E, and F always live in database Y".

是有一个更简单的方法来设置这样的默认值?例如,作为模型元字段?

Is there a simpler way to set defaults like this? For instance, as a model meta field?

推荐答案

没有 Meta 这个字段(有一点在某一点,但由于其引入的限制而被删除)。您需要一个数据库路由器来控制哪些对象去什么数据库。在你的情况下,路由器应该很容易实现。

There is no Meta field for this (there was one at some point but it got removed because of the limitations it introduced). You need a database router to control which objects go to what database. In your case the router should be pretty easy to implement.

这篇关于在django中为每个模型设置db的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆