在Django应用程序中使用Python对象(Using Python Objects in Django Applications)

编程入门 行业动态 更新时间:2024-10-08 10:59:10
在Django应用程序中使用Python对象(Using Python Objects in Django Applications) python

如果这看起来像一个愚蠢的问题我很抱歉,但我仍然是一个新手Python / Django程序员。 在Django应用程序中创建不是将保存在数据库中的模型的Python对象是否正常?

我正在创建一个相当大的Django应用程序,对我来说,我的代码真的开始“闻”了。 我的意思是我的观点变得非常大,因为我采用的是程序性而非面向对象的方法。 我的直觉告诉我,如果我使用更多具有自己属性和行为的对象而不是将信息从一个函数传递到我的视图中的下一个,我的代码可能更简单,更容易测试,并且从长远来看更加健壮。

让我感到高兴的是,这些不是我想保存在我的数据库中的对象所以我不知道我是否应该使用它们,如果我应该,我会把它们放在哪里。 我在Django应用程序中提出的方法是典型的吗? 如果是这样,我将在哪里存储与Django模型/视图/模板结构相关的对象? 此外,是否有任何流行的Django模块或库,我正在描述我应该学习的东西?

在此先感谢您的回复。

I apologize if this seems like a stupid question but I'm still very much a novice Python/Django programmer. Is it normal to create Python objects in a Django application that aren't models that will be saved in the database?

I'm creating what's become a fairly large Django application and, to me, my code is really starting to "smell". What I mean is that my views are becoming very large because I'm taking a procedural rather than object-oriented approach. My intuition tells me that my code might be simpler, easier to test, and more robust in the long run if I were using more objects with their own attributes and behaviors rather than passing information from one function to the next in my views.

What's hanging me up is that these aren't objects I want to save in my database so I don't quite know if I should be using them and, if I should, where I'd put them. Is the approach I'm proposing typical in a Django application? If so, where would I store those objects with respect to the Django model/view/template structure? Also, are there any popular Django modules or libraries that do what I'm describing that I should study?

Thanks in advance for your response.

最满意答案

您可以将对象存储在任何位置。 视图文件或模型文件或任何地方都可能有辅助函数。 我更喜欢将各种函数放在utils.py文件中,但这不是约定,只是我最终要做的事情。 我最终将大多数杂项辅助函数和基类放在一个common应用程序中,更具体地说是一个common.utils文件。

在一个项目中,我有很多应用程序,每个应用程序都有一个api客户端。 客户端的基类驻留在名为common的应用程序中。 然后每个应用程序在client.py文件中都有他们的特定客户端

项目 共同 客户 APP1 客户 APP2 客户

然后在app1客户端

from project.common.client import BaseClient class ConcreteApp1Client(BaseClient): pass

然后在我的视图或管理命令或模型中,或者可以正常导入和使用具体客户端的任何位置。 from project.app1.client import ConcreteApp1Client

You can store your objects anywhere. There could be helper functions in your views file or models file or wherever. I prefer to put miscellaneous functions in a utils.py file but that is not a convention, just something I end up doing. I end up putting most of miscellaneous helper functions and base classes in a common app, and more specifically a common.utils file.

In one project I have lots of apps, and each app has an api client. The base class for the client resides in an app called common. Then each app then has their specific client in client.py file

project common client app1 client app2 client

Then in app1 client

from project.common.client import BaseClient class ConcreteApp1Client(BaseClient): pass

Then in my views or management commands or models or wherever the concrete client can be imported and used as normal. from project.app1.client import ConcreteApp1Client

更多推荐

本文发布于:2023-04-29 07:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1335595.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:应用程序   对象   Django   Python   Applications

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!