简体   繁体   English

Django,项目结构

[英]Django, project structure

I'm currently working on a django project. 我目前正在研究一个django项目。 I'm not completly new in django, but have some difficulties figuring out how would be the most 'djangonic' way to organize some files. 我不是django中的新手,但是在确定如何组织一些文件的最“混合”方式方面有一些困难。

I have some classe that compute stuff, and can be used from the manage.py's cli, and by a webservice app. 我有一些计算东西的classe,可以从manage.py的cli和webservice应用程序中使用。 Those computation uses models from my core app and make call to the database. 这些计算使用我的核心应用程序中的模型并调用数据库。

The problem I face is that I can't figure where to put those source file. 我面临的问题是我无法确定将源文件放在何处。

I do not want to put them in a 'lib' folder. 我不想把它们放在'lib'文件夹中。 I think the modules in this folder will be django independent modules. 我认为这个文件夹中的模块将是django独立模块。

For the moment, the module is in the model of my app. 目前,该模块是我的应用程序的模型。 But since it doesn't define any new structure, I don't think it is the cleanest way. 但由于它没有定义任何新的结构,我认为这不是最干净的方式。

Any ideo to have a clean structure that respect the 'django way of life' ? 有什么意识到拥有一个尊重'django生活方式'的干净结构?

Thanks 谢谢

The thing is, the most "djangonic" way of doing this is not to have a "core app" at all. 问题是,最“djangonic”的做法是不要有“核心应用程序”。 You should strive to split the functionality into separate apps. 您应该努力将功能拆分为单独的应用程序。 I'm perfectly aware that this is not always trivial. 我完全清楚这并不总是微不足道的。

But let's say you are designing an intranet website for a school. 但是,假设您正在为学校设计Intranet网站。 You would have one app that deals with students' accounts, and another app generating timetables, and yet another one for an internal message board, etc. Every app defines its own models (there are no "project level models"), but apps can import each others models (so message board posts can have a ForeignKey field pointing at student from the "students" app). 您将有一个应用程序处理学生的帐户,另一个应用程序生成时间表,另一个应用程序生成内部留言板等。每个应用程序定义自己的模型(没有“项目级模型”),但应用程序可以导入彼此模型(因此留言板帖子可以有一个指向来自“学生”应用程序的学生的ForeignKey字段)。 There is no need for a "main" or "core" app (see also: James Bennett's "writing reusable Django applications" presentation from DjangoCon) 不需要“主要”或“核心”应用程序(另请参阅: James Bennett的“编写可重复使用的Django应用程序”演示文稿来自DjangoCon)

You would then put your management commands in management/commands directory of an app that the commands deal with. 然后,您可以将管理命令放在命令处理的应用程序的management/commands目录中。 For example if a command removes old students from the db it would go into management/commands inside the students app. 例如,如果命令从数据库中删除旧学生,它将进入students应用程序内的management/commands

If the module is independent from Django, then it should be a standalone Python package, complete with a setup.py to install it. 如果该模块独立于Django,那么它应该是一个独立的Python包,并配有setup.py来安装它。 The Django models and webservices that use it can import like any other external dependencies. 使用它的Django模型和Web服务可以像任何其他外部依赖项一样导入。

If the module depends on you Django app, then it should be inside the app's directory. 如果模块依赖于你的Django应用程序,那么它应该在应用程序的目录中。 If it doesn't define new models, then it should not be in models.py . 如果它没有定义新模型,那么它不应该在models.py

Does this answer your question? 这回答了你的问题了吗?

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

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