Django在哪里放置共享模型和逻辑

时间:2021-04-18 18:06:52

I Have the following folder setup:

我有以下文件夹设置:

  • myproject
  • web
  • worker

The web project is a website containing the typical django setup and all, including the model and some service classes which move the logic away from the views. The website will display data coming from a database. The worker folder contains 2 classes which are filling the database and aggregate it. These 2 classes are like background processes. My question is, how should I structure this?

Web项目是一个包含典型django设置的网站,包括模型和一些将逻辑从视图中移开的服务类。该网站将显示来自数据库的数据。 worker文件夹包含2个类,这些类填充数据库并对其进行聚合。这两个类就像后台进程。我的问题是,我应该如何构建这个?

  • Should each worker class get his own application folder
  • 每个工人类应该获得自己的应用程序文件夹

  • If so, I would prefer to move the models out of the web project in the myproject folder, since they are shared between the applications. However this seems to be against the django convention, why so? And how would the convention handle this?
  • 如果是这样,我宁愿将模型移出myproject文件夹中的web项目,因为它们是在应用程序之间共享的。然而,这似乎是反对django惯例,为什么呢?会议将如何处理这个问题?

  • If not, where should I put these worker processes? And how should I run them ?
  • 如果没有,我应该在哪里放置这些工作流程?我该怎么办呢?

Thanks in advance!

提前致谢!

2 个解决方案

#1


0  

Usually if I have stuff to be shared between different sections I either: 1) make a lib and install it via pip 2) make a helper folder within the project

通常,如果我有不同部分之间共享的东西,我要么:1)创建一个lib并通过pip安装它2)在项目中创建一个帮助文件夹

#2


0  

Keep the project and the app(s) separate and create proper Python packages to handle dependencies. I.e.

保持项目和应用程序分开,并创建适当的Python包来处理依赖项。即

src/sites/web   <-- the project, containing manage.py, settings and root urls

and

src/apps/myapp  <-- the app, containing models, views, etc
src/apps/myapp/worker  <-- strongly related functionality

in src/sites/web/setup.py (and/or requirements.txt) you would add myapp as an (install) dependency.

在src / sites / web / setup.py(和/或requirements.txt)中,您将myapp添加为(安装)依赖项。

If worker is only loosely related functionality, then a separate package would make sense, e.g.:

如果worker只是松散相关的功能,那么单独的包是有意义的,例如:

src/lib/worker

where src/lib/worker/setup.py (and/or requirements.txt) would add myapp as a dependency, and src/sites/web/setup.py would add worker as a dependency.

其中src / lib / worker / setup.py(和/或requirements.txt)会将myapp添加为依赖项,而src / sites / web / setup.py会将worker添加为依赖项。

#1


0  

Usually if I have stuff to be shared between different sections I either: 1) make a lib and install it via pip 2) make a helper folder within the project

通常,如果我有不同部分之间共享的东西,我要么:1)创建一个lib并通过pip安装它2)在项目中创建一个帮助文件夹

#2


0  

Keep the project and the app(s) separate and create proper Python packages to handle dependencies. I.e.

保持项目和应用程序分开,并创建适当的Python包来处理依赖项。即

src/sites/web   <-- the project, containing manage.py, settings and root urls

and

src/apps/myapp  <-- the app, containing models, views, etc
src/apps/myapp/worker  <-- strongly related functionality

in src/sites/web/setup.py (and/or requirements.txt) you would add myapp as an (install) dependency.

在src / sites / web / setup.py(和/或requirements.txt)中,您将myapp添加为(安装)依赖项。

If worker is only loosely related functionality, then a separate package would make sense, e.g.:

如果worker只是松散相关的功能,那么单独的包是有意义的,例如:

src/lib/worker

where src/lib/worker/setup.py (and/or requirements.txt) would add myapp as a dependency, and src/sites/web/setup.py would add worker as a dependency.

其中src / lib / worker / setup.py(和/或requirements.txt)会将myapp添加为依赖项,而src / sites / web / setup.py会将worker添加为依赖项。