直接在Model类上使用Django Managers和staticmethod [英] Using Django Managers vs. staticmethod on Model class directly

查看:73
本文介绍了直接在Model类上使用Django Managers和staticmethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在阅读了Django Managers之后,我仍然不确定使用它会带来多少好处。似乎最好的用法是添加自定义查询(只读)方法,例如 XYZ.objects.findBy *()。但是我可以使用 Model 类本身的静态方法轻松地做到这一点。

After reading up on Django Managers, I'm still unsure how much benefit I will get by using it. It seems that the best use is to add custom queries (read-only) methods like XYZ.objects.findBy*(). But I can easily do that with static methods off of the Model classes themselves.

我总是更喜欢后者,因为:

I prefer the latter always because:


  1. 在可读性和易于维护方面的代码局部性

  2. 我不那么冗长我的通话中不需要 objects 属性

  3. Manager 类具有奇怪的规则关于模型继承,最好还是避免。

  1. code locality in terms of readability and easier maintenance
  2. slightly less verbose as I don't need the objects property in my calls
  3. Manager classes have weird rules regarding model inheritance, might as well stay clear of that.

是否有任何合理的理由使用静态方法,而是使用Manager类?

Is there any good reason not to use static methods and instead use Manager classes?

推荐答案

将自定义查询添加到管理器是Django约定。从自定义管理员上的Django文档中:

Adding custom queries to managers is the Django convention. From the Django docs on custom managers:


添加额外的Manager方法是向模型添加表级功能的首选方法。

Adding extra Manager methods is the preferred way to add "table-level" functionality to your models.

如果它是您自己的私有应用程序,那么约定俗成的词没什么大不了的-实际上我公司的内部代码库中有一些类方法可能属于定制管理器。

If it's your own private app, the convention word doesn't matter so much - indeed my company's internal codebase has a few classmethods that perhaps belong in a custom manager.

但是,如果您要编写要与其他Django用户共享的应用程序,那么他们期望看到 findBy 在自定义管理器上。

However, if you're writing an app that you're going to share with other Django users, then they'll expect to see findBy on a custom manager.

我认为您提到的继承问题不是太糟糕。如果您阅读自定义管理器和模型继承文档,我认为您不会陷入困境。编写 .objects 的冗长性是可以忍受的,就像我们使用 XYZ.objects.get()和 XYZ.objects.all()

I don't think the inheritance issues you mention are too bad. If you read the custom managers and model inheritance docs, I don't think you'll get caught out. The verbosity of writing .objects is bearable, just as it is when we do queries using XYZ.objects.get() and XYZ.objects.all()

在我看来,使用管理器方法有一些优点:

Here's a few advantages of using manager methods in my opinion:


  1. API的一致性。您的方法 findBy 属于 get filter 总计,其余的。是否想知道可以在 XYZ.objects 管理器中执行的查找?您可以使用 dir(XYZ.objects)进行内省很简单。

  1. Consistency of API. Your method findBy belongs with get, filter, aggregate and the rest. Want to know what lookups you can do on the XYZ.objects manager? It's simple when you can introspect with dir(XYZ.objects).

静态方法会混乱实例名称空间。 XYZ.findBy()很好,但是如果定义静态方法,也可以执行 xyz.findBy() 。在特定实例上运行 findBy 查找实际上没有任何意义。

Static methods "clutter" the instance namespace. XYZ.findBy() is fine but if you define a static method, you can also do xyz.findBy(). Running the findBy lookup on a particular instance doesn't really make sense.

DRYness。有时您可以在多个模型上使用同一管理器。

DRYness. Sometimes you can use the same manager on more than one model.

说了这么多,完全取决于您。我不知道您不应该使用静态方法的致命原因。您是成年人,这是您的代码,如果您不想编写 findBy 作为管理器方法,那么天空不会掉下来;)

Having said all that, it's up to you. I'm not aware of a killer reason why you should not use a static method. You're an adult, it's your code, and if you don't want to write findBy as a manager method, the sky isn't going to fall in ;)

如需进一步阅读,我建议您发布博客文章管理器与类方法,Django发布管理器James Bennett。

For further reading, I recommend the blog post Managers versus class methods by James Bennett, the Django release manager.

这篇关于直接在Model类上使用Django Managers和staticmethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆