跨多个项目/微服务的Django模型.如何? [英] Django models across multiple projects/microservices. How to?

查看:796
本文介绍了跨多个项目/微服务的Django模型.如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何解决多个(分离的)django项目/微服务之间的模型结构共享.例如:

I'm wondering how to solve sharing of the model structure between multiple (separated) django projects/microservices. Eg:

  1. 项目:API
  2. 项目:用户仪表板
  3. 项目:管理控制台
  4. 项目:统计信息

每个项目都使用相同的django模型.有没有一种合适的方法来解决这个问题?

Each of that projects uses the same django models. Is there a one, proper way to solve that?

推荐答案

Django的基本想法是将整个应用程序功能耦合在一起,但这并不适合您.这是一个风格和意见问题,这是我在类似情况下的做法.

Django basic idea is to couple the entire app functionality together, but this does not hold in your case. It's a style and opinion question, here is what I did in a similar situation.

将应用程序功能分为两个项目巢穴:

Split the app functionality into two project lairs:

mysite
   |
   | - db_access
   |         | --- app1
   |                 | ---- models.py
   |                 | ---- db_api.py
   |         | --- app2
   |                 | ---- models.py
   |                 | ---- db_api.py
   | - service
   |         | --- app1
   |                 | ---- urls.py
   |                 | ---- views.py
   |         | --- app2
   |                 | ---- urls.py
   |                 | ---- views.py

db_access部分包含模型,而db_api.py部分包含查询,获取对象等,因此您无需查询模型,但可以查询db_api.

The db_access part has the models, and db_api.py has the queries, get objects etc, so you don't query the models, but the db_api.

代替

item = app1.models.Items.objects.get(user=request.user)

使用

item = app1.db_api.get_first_item(user=request.user)

此样式可让您共享数据库&模型化访问,而每个服务消耗其所需的东西,然后将其用于API,网站等.如果服务以其他服务不(也不会)使用的方式消耗某些数据,则将此查询放在服务代码,否则在db_api.py中.

This style lets you share the db & models access together, while each service consumes what it needs, and then use it for API, website etc. If a service consumes some data in a way that other services don't (and will not) use, then put this query in the service code, otherwise in the db_api.py.

这更像是一个传统的应用程序,但是它可以工作.

This is more like a traditional laired application, but it works.

另一件事,同一项目可以使用两个 git存储库,一个用于db_access(所有服务都将其拉出),一个用于特定服务.因此,每个django项目实际上都是db_access存储库,而服务存储库-如果项目代码来自两个存储库,则wsgi不在乎.

Another thing, the same project can use two git repositories, one for the db_access (which all services pull), and one for the specific service. So each django project is actually the db_access repo, and the service repo - wsgi doesn't care, of course, if the project code comes from two repositories.

这篇关于跨多个项目/微服务的Django模型.如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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