django 中业务逻辑放在哪里

发布于 2024-10-31 15:01:30 字数 587 浏览 1 评论 0原文

例如账户1--> *用户--> 1 认证 1个帐户有多个用户,每个用户将有1次身份验证

我来自java背景,所以我通常做的是

  1. 将这些类定义为java beans(即,只是getter和setter,没有附加逻辑)
  2. 创建AccountManager ejb类,定义create_account方法(需要1个帐户,用户列表)
  3. 在web层准备数据,然后将数据传递到AccountManager ejb中,例如: accountManager.createAccount(account, userList)

但在 django 中,框架提倡将域逻辑放入模型类(行级)或关联的管理器类(表级)中,这让事情变得有点尴尬。是的,如果你的逻辑只涉及一张表是可以的,但在实际应用中,通常每一步都会涉及多个不同的表甚至数据库,那么这种情况下我该怎么办呢?

将逻辑放入视图中?我认为这根本不是一个好的做法。或者甚至覆盖模型类中的 save 方法,使用 **kwargs 传递额外的数据?那么后端就会崩溃。

我希望这能说明我对业务逻辑应放置在 django 应用程序中的位置的困惑。

For example, Account 1--> *User --> 1 Authentication
1 account has multiple users and each user will have 1 authentication

I come from java background so what I usually do is

  1. define these classes as java beans (ie, just getter and setter, no logic attached)
  2. create AccountManager ejb class, define create_account method (with takes 1 account, list of users)
  3. preparing data in the web layer, then pass data into AccountManager ejb, for instance:
    accountManager.createAccount(account, userList)

But in django, the framework advocates that you put domain logic into model classes (row level) or associated manager classes (table level), which makes things bit awkward. Yes, it is fine that if your logic is only involves one table, but in the real application, usually each step will involve multiple different tables or even databases, so what should I do in this case?

Put the logic into View? I don't think this is good practice at all. or even overwrite the save method in model class, passing in extra data by using **kwargs? then the backend will break.

I hope this illustrates my confusion with where business logic should be placed in a django application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

℡寂寞咖啡 2024-11-07 15:01:30

不确定您是否已阅读 Django 中的 管理器 部分,看来可以解决你目前的情况。假设您定义了以下 Account 模型,则 User 是内置的。

# accounts/models.py

class AccountManager(models.Manager):
    def create_account(self, account, user_list):
        ...

class Account(models.Model):
    objects = AccountManager()

如果管理器代码太大,请随意将其分离到单独的文件中。在您看来:

# views.py

from accounts.models import Account

Account.objects.create_account(account, user_list)

业务逻辑仍然在模型中。

编辑

这里的关键字是override,而不是overwrite。如果您覆盖模型的保存方法,则必须记住,Web 应用程序和管理员中的任何创建、更新操作都将使用此新功能。如果您只希望这些业务逻辑在特定视图中发生一次,那么最好将其排除在save 之外。

我想你可以将你的业务逻辑放在它自己的常规类中。每次需要运行业务逻辑时,您都必须实例化该类。或者,如果您想跳过 OOP 方法,则可以将业务逻辑作为静态函数放入这个新类中。

Not sure if you've read the section on Managers in Django, it seems to solve your current situation. Assuming you have the following Account model defined, User is built-in.

# accounts/models.py

class AccountManager(models.Manager):
    def create_account(self, account, user_list):
        ...

class Account(models.Model):
    objects = AccountManager()

Feel free to separate your manager code in a separate file if it gets too big. In your views:

# views.py

from accounts.models import Account

Account.objects.create_account(account, user_list)

The business logic is still in the models.

EDIT

The keyword here is override, not overwrite. If you override the model's save method, you have to keep in mind that any create, update operations from within your web app and admin will use this new functionality. If you only want those business logic to happen once in a specific view, it might be best to keep it out of save.

I guess you can put your business logic in its own regular class. You will have to instantiate that class each time you need to run your business logic. Alternatively, you can put your business logic as a static function in this new class if you want to skip the OOP approach.

美人迟暮 2024-11-07 15:01:30

我正在做的是,我的大多数应用程序都有带有业务逻辑的 service.py 文件。这是因为,如果我需要一个与多个应用程序的模型一起使用的函数,我根本无法做到这一点:

# shop/models.py
from auth.models import User, Team

此代码将陷入循环引用循环中。

但我很可能会从 service.py 转移到具有如下结构的应用程序:

service/
    auth.py
    customers.py
    another_set_of_functions.py
    ...

What I'm doing is that most of my apps have service.py file with business logic. This is because if I need a function that works with models from multiple apps I simply cannot do this:

# shop/models.py
from auth.models import User, Team

This code will be stuck in circular reference loop.

But I'm gonna move most likely from service.py to app with structure like this:

service/
    auth.py
    customers.py
    another_set_of_functions.py
    ...
沉鱼一梦 2024-11-07 15:01:30

上面用 accountManager.createAccount(account, userList) 举例说明的示例似乎可以通过向帐户模型添加 createAccount 方法轻松完成。如果您觉得需要将业务逻辑置于模型之外,那么您始终可以创建一个托管业务逻辑的新模块,然后在视图中导入并使用。

Well the example that you illustrated above with the accountManager.createAccount(account, userList) seems like something that could be easily done my adding a createAccount method to the account model. If you feel the need to take business logic outside of the model though you could always just create a new module that host your business logic, and then import and used in in your views.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文