简体   繁体   English

保持许多不同的Django模型类型选择为DRY的最佳方法是什么?

[英]What is the best approach to keep many different django model type choices DRY?

I decoupled my system into many applications where each of those have many models from which many have 我将系统解耦到许多应用程序中,其中每个应用程序都有许多模型,许多模型都有

This is the structure. 这就是结构。

ROOT

  • core (for keeping generic stuff) 核心(用于保留通用内容)
  • decoupled app 解耦的应用程序
  • decoupled app 解耦的应用程序
  • decoupled app 解耦的应用程序
  • ... ...
  • decoupled app 解耦的应用程序

In those apps many times models have model type choices. 在这些应用程序中,模型通常会选择模型类型。 For example I have many models like this Event and Resource model. 例如,我有很多类似事件和资源模型的模型。

VIDEO = 1
AUDIO = 2
IMAGE = 3
ARTICLE = 4
BOOK = 5
DOCUMENT = 6

RESOURCE_CONTENT_CHOICES = (
    (VIDEO, _("video")),
    (AUDIO, _("audio")),
    (IMAGE, _("image")),
    (ARTICLE, _("article")),
    (BOOK, _("book")),
    (DOCUMENT, _("document")),
)
class Resource(models.Model):
    title = models.CharField(max_length=256)
    resource_content = models.IntegerField(choices=RESOURCE_CONTENT_CHOICES)
    url = models.URLField()

PAST = 1
PRESENT = 2
FUTURE = 3

EVENT_TYPE_CHOICES = (
    (PAST, _("past")),
    (PRESENT, _("present")),
    (FUTURE, _("future")),
)
class Event(models.Model):
    title = models.CharField(max_length=256)
    event_type = models.IntegerField(choices=EVENT_TYPE_CHOICES)

Moreover many times tuples like EVENT_TYPE_CHOICES are used in other models as well. 而且,在其他模型中也多次使用了像EVENT_TYPE_CHOICES这样的元组。 And I have around 20 other models with event_type like type field. 我还有大约20个其他带有event_type模型,例如type field。

I was thinking that I could create types.py file in core app and write one dict for all types in one place so I can easily use in views, other models etc. because apps are not going to be reused in other projects anyway. 我当时想我可以在核心应用程序中创建types.py文件,并在一处为所有类型编写一个字典,以便我可以轻松地在视图,其他模型等中使用,因为应用程序无论如何都不会在其他项目中重用。 But I'm not sure if this is the best approach. 但是我不确定这是否是最好的方法。

So what would be the best approach to make everything scalable and DRY? 那么使所有内容都可扩展和DRY的最佳方法是什么?

It depends on your intended use of the code. 这取决于您对代码的预期用途。

The goal in decoupling apps is to isolate functionality that is expected to be generally reusable across many Django projects. 解耦应用程序的目标是隔离通常可在许多Django项目中重用的功能。 However, you mentioned that: 但是,您提到了:

"...your apps are not going to be reused in other projects anyway" “ ...您的应用程序无论如何都不会在其他项目中重用”

If that is the case, especially if the apps aren't considered to be good candidates for reuse, then strictly decoupling the apps isn't necessary. 如果真是这样,尤其是如果这些应用程序不被认为是很好的重用候选者,那么就不必严格地将这些应用程序分离。 Therefore, use a single Python file in the core of your app which contains the various choices lists used by multiple models. 因此,请在应用程序的核心中使用一个Python文件,其中包含多个模型使用的各种选择列表。

Like this: 像这样:

# choices.py
RESOURCE_CONTENT_CHOICES = (
    ...
)
EVENT_TYPE_CHOICES = (
    ...
)

And import where needed: 并在需要的地方导入:

# models.py
from myproject.core.choices import EVENT_TYPE_CHOICES

class Event(models.Model):
    # Optional: See additional note below
    EVENT_TYPE_CHOICES = EVENT_TYPE_CHOICES
    ...
    event_type = models.IntegerField(choices=EVENT_TYPE_CHOICES)
    ...

Alternatively, if you discover some of your apps are indeed good candidates for reuse, then decoupling them is more important than DRY. 另外,如果您发现某些应用程序确实是重用的不错选择,那么将它们去耦比DRY更重要。 In that scenario, you should maintain your choices inside that app, even if some choices are duplicated elsewhere in other apps. 在这种情况下,即使某些选择在其他应用程序的其他地方重复,您也应在该应用程序内维护选择。

Additional note: using either approach, it's often very useful to have a reference to the choices within the model class. 补充说明:使用这两种方法时,引用模型类中的选择通常非常有用。 See: https://docs.djangoproject.com/en/1.7/ref/models/fields/#choices 参见: https : //docs.djangoproject.com/en/1.7/ref/models/fields/#choices

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

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