简体   繁体   English

Django模型跨多个项目/微服务。 如何?

[英]Django models across multiple projects/microservices. How to?

I'm wondering how to solve sharing of the model structure between multiple (separated) django projects/microservices. 我想知道如何解决多个(分离的)django项目/微服务之间的模型结构共享。 Eg: 例如:

  1. Project: API 项目:API
  2. Project: Users dashboard 项目:用户仪表板
  3. Project: Admin dashboard 项目:管理仪表板
  4. Project: Statistics 项目:统计

Each of that projects uses the same django models. 每个项目都使用相同的django模型。 Is there a one, proper way to solve that? 是否有一种正确的解决方法?

Django basic idea is to couple the entire app functionality together, but this does not hold in your case. Django的基本思想是将整个应用程序功能结合在一起,但这并不适用于您的情况。 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

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. db_api.py部分具有模型, db_api.py具有查询,获取对象等,因此您不查询模型,而是查询db_api。

Instead of 代替

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

Use 使用

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

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. 这种风格允许您一起共享数据库和模型访问,同时每个服务消耗它所需的内容,然后将其用于API,网站等。如果服务以某种方式消耗某些数据,而其他服务不会(并且不会)使用,然后将此查询放在服务代码中,否则放在db_api.py中。

This is more like a traditional laired application, but it works. 这更像是传统的应用程序,但它可以工作。

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. 另一件事,同一个项目可以使用两个 git存储库,一个用于db_access(所有服务都拉),一个用于特定服务。 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项目实际上都是db_access repo,当然,如果项目代码来自两个存储库,那么服务repo-wsgi并不关心。

Two potential options: 两个可能的选择:

Option 1 - Use a common codebase and deploy multiple instances of it (one for each microservice). 选项1 - 使用公共代码库并部署它的多个实例(每个微服务一个)。 You could follow 12factor Apps Methodology and just load a different set of environment variables for each deployed instance. 您可以遵循12factor Apps Methodology,只为每个已部署的实例加载一组不同的环境变量。 http://12factor.net/ http://12factor.net/

Option 2 - Split your django code up into self contained applications then have a project for each microservice that just includes these projects and a url config. 选项2 - 将您的django代码拆分为自包含的应用程序,然后为每个包含这些项目和url配置的微服务创建一个项目。

I can expand further if this is helpful? 如果这有用,我可以进一步扩展吗?

If you want to use same models in different python apps (not modules, but apps, with different uwsgi-instances etc.), then I think that the much common and handy solution - provide interface for these models through some API: 如果你想在不同的python应用程序中使用相同的模型(不是模块,但是应用程序,具有不同的uwsgi实例等),那么我认为这是一个非常常见且方便的解决方案 - 通过一些API为这些模型提供接口:

  1. You can build REST interface in each of your projects, and then call appropriate API methods to get result. 您可以在每个项目中构建REST接口,然后调用适当的API方法来获取结果。
  2. You can use Celery package to build internal API based on the AMQP transport. 您可以使用Celery包基于AMQP传输构建内部API。 Then you need to configure your apps to listen some RabbitMQ queue which will be used as message storage for your projects communnication. 然后,您需要配置您的应用程序以侦听一些RabbitMQ队列,该队列将用作项目通信的消息存储。

Django models is just a Python module, of course Django does a lot of 'magic' in other modules (ie forms, admin) using introspection, but the models itself could be used separately if desired. Django模型只是一个Python模块,当然Django使用内省在其他模块(即表单,管理员)中做了很多“魔术”,但如果需要,模型本身可以单独使用。

So there's no problem... If you use different "Django projects" for each project you named (on same machine or different), then just make your models as separate "Django app" and set each project to use it. 所以没有问题......如果你为你命名的每个项目(在同一台机器上或不同的机器上)使用不同的“Django项目”,那么只需将你的模型作为单独的“Django应用程序”,并设置每个项目使用它。 If each project named by you is "Django app" (in this case on same machine obviously) then just import your models module in each "project" and use it. 如果您指定的每个项目都是“Django app”(在这种情况下显然在同一台机器上),那么只需在每个“项目”中导入模型模块并使用它。 Even if you don't use Django, say your "API project" is based on Flask, you can still import Django models in Flask modules. 即使您不使用Django,说您的“API项目”基于Flask,您仍然可以在Flask模块中导入Django模型。

Behind the scene of Django models there's the database, which doesn't care from where it is queried, and Django just provides a convenient way to use the DB via Python classes. 在Django模型的背后,有数据库,它不关心查询的位置,Django只是提供了一种通过Python类使用数据库的便捷方式。 So if the models from different apps or projects are set to use the same DB, then they will be 'shared'. 因此,如果来自不同应用程序或项目的模型设置为使用相同的数据库,那么它们将被“共享”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM