简体   繁体   English

将模型与应用程序的其余部分分开是一种很好的编程习惯

[英]Is it a good programming practice to separate models from the rest of the application

My project consists of several django applications that need to be deployed differently, possibly on different machines. 我的项目包含几个需要以不同方式部署的django应用程序,可能在不同的机器上。 However often these apps occasionally need to access each other's models, so I was thinking of "externalizing" my models so that they can be accessed more elegantly from any app. 然而,这些应用程序通常偶尔需要访问彼此的模型,所以我想要“外化”我的模型,以便可以从任何应用程序更优雅地访问它们。 So the idea is to do have directory structure resembling something like this: 所以我的想法是让目录结构类似于这样:

/ 
+ application1
+ application2
+ models

Is there a functional point to doing that (other than code maintainability), since the applications can cross-reference each other? 这样做是否具有功能性(代码可维护性除外),因为应用程序可以相互交叉引用?

The following paragraph in the django book makes me think that that's probably not a good idea (I added the bold formatting): django书中的以下段落让我觉得这可能不是一个好主意(我添加了粗体格式):

However, there's one requirement regarding the app convention: if you're using Django's database layer (models), you must create a Django app. 但是,关于应用程序约定有一个要求:如果您使用的是Django的数据库层(模型),则必须创建一个Django应用程序。 Models must live within apps . 模特必须住在应用程序中 Thus, in order to start writing our models, we'll need to create a new app. 因此,为了开始编写我们的模型,我们需要创建一个新的应用程序。

The following didn't fit well in the comments to @jcollado's answer so I'll put it here: 以下不适合@ jcollado答案的评论,所以我会把它放在这里:

https://docs.djangoproject.com/en/dev/topics/db/models/#models-across-files https://docs.djangoproject.com/en/dev/topics/db/models/#models-across-files

Models across files 跨文件的模型

It's perfectly OK to relate a model to one from another app. 将模型与另一个应用程序中的模型相关联是完全可以的。 To do this, import the related model at the top of the model that holds your model. 为此,请在包含模型的模型顶部导入相关模型。 Then, just refer to the other model class wherever needed. 然后,只需在需要的地方引用其他模型类。 For example: 例如:

from geography.models import ZipCode

class Restaurant(models.Model):
    # ...
    zip_code = models.ForeignKey(ZipCode)

I don't think this is a particularly good idea, though I can see the appeal. 我不认为这是一个特别好的主意,虽然我可以看到吸引力。 You will be including a lot of models you're not using if you only want one of your apps installed. 如果您只想安装一个应用程序,那么您将包含很多未使用的模型。 I think it's best to keep models within the app they most relate to. 我认为最好将模型保存在与他们最相关的应用程序中。

This also may make using the admin interface more confusing. 这也可能使管理界面更加混乱。 Where do you register the models with the admin? 你在哪里注册模特管理员? Where do you do the admin customization for a model? 您在哪里为模型进行管理员自定义?

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

相关问题 Pydantic 在 FastAPI 中模拟良好实践 - Pydantic models good practice in FastAPI Python 枚举列表的良好编程习惯 - Python good programming practice for enumerating lists 用于结束在列表中查找项目的循环的良好 Python 编程实践 - Good Python programming practice for ending a loop on finding an item in a list Python 中的嵌套 try/except 块是一种好的编程习惯吗? - Are nested try/except blocks in Python a good programming practice? 在 Web 应用程序中发送架构是一个好习惯吗? - Is it a good practice to send schema in web application? 在Django应用程序的内存中存储数据是一种好习惯吗? - is it a good practice to store data in memory in a django application? 从上下文管理器中收益是一种好的做法吗? - Is it good practice to yield from within a context manager? 编写Python GTK +应用程序的“良好实践”方法是什么? - What is a 'good practice' way to write a Python GTK+ application? 有一个函数返回字典是一种好的编程习惯吗? (以便旧代码不会中断) - Is it good programming practice to have a function return a dictionary ? (so that old code doesn't break) 来自 Hackerearth 的带有运行时错误的基本编程 Python3 练习题 - Basic Programming Python3 practice question with runtime error from Hackerearth
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM