如何使用Django模型字段定义保持DRY

时间:2021-04-26 17:03:09

What is the best practice for observing DRY principles while defining Django model fields.

在定义Django模型字段时观察DRY原则的最佳实践是什么。

Scenario 1:

file_one = models.FilePathField(path=FIELD_PATH, allow_files=True, allow_folders=True, recursive=True)
file_two = models.FilePathField()
file_three = models.FilePathField()

Can I do this:

我可以这样做:

file_one = models.FilePathField(path=FIELD_PATH, allow_files=True, allow_folders=True, recursive=True)
file_two = file_one
...

Scenario 2:

base = models.FilePathField(allow_files=True, allow_folders=True, recursive=True)
file_one = models.FilePathField(path=FIELD_PATH1)
file_two = models.FilePathField(path=FIELD_PATH2)
file_three = models.FilePathField(path=FIELD_PATH3)

How can I have file_one, _two and _three inherit/extend the rules in base = models... while being able to assign each a different path=...

如何让file_one,_two和_three继承/扩展base = models中的规则...同时能够为每个路径分配不同的路径= ...

I feel like Django: Dynamic model field definition is close but not quite what I'm looking for!

我觉得像Django:动态模型字段定义很接近,但不是我想要的!

Stay awesome Stack Overflow!

保持棒极了Stack Overflow!

2 个解决方案

#1


4  

I agree with Pete that you definitely don't want to get too tricksy with a simple model definition. You could make the multiple nearly-the-same file fields a little easier to manage and still be explicit by keeping your defaults in a dictionary and using the ** operator. Something like:

我同意Pete的观点,你绝对不希望通过简单的模型定义过于狡猾。您可以通过将默认值保存在字典中并使用**运算符来使多个几乎相同的文件字段更容易管理并且仍然是明确的。就像是:

filefield_defaults = {
    'allow_files':True, 
    'allow_folders':True, 
    'recursive':True
}

file_one = models.FilePathField(
    path=FIELD_PATH1,
    **filefield_defaults
)

file_two = models.FilePathField(
    path=FIELD_PATH2,
    **filefield_defaults
)

#2


5  

Honestly, DRY code is important and should be strived for, but there are limits :) In this case you're at odds between DRY and line two of the zen of python Explicit is better than implicit. If I was maintaining your code I'd much rather come in and see:

老实说,DRY代码很重要,应该努力,但有限制:)在这种情况下,你在DRY和蟒蛇明确的第二行之间的差异优于隐式。如果我维护你的代码,我宁愿进来看看:

file_one = models.FilePathField(path=FIELD_PATH1, allow_files=True, allow_folders=True, recursive=True)
file_two = models.FilePathField(path=FIELD_PATH2, allow_files=True, allow_folders=True, recursive=True)
file_three = models.FilePathField(path=FIELD_PATH3, allow_files=True, allow_folders=True, recursive=True)

Because while not being "DRY", it is immediately obvious what is going on, and I don't have to waste time going "wait, what?"

因为虽然不是“干”,但是很明显发生了什么,我不必浪费时间去“等,什么?”

(Actually strictly speaking what I'd like to see is:

(实际上严格来说,我希望看到的是:

# Useful comments
file_one = models.FilePathField(
    path=FIELD_PATH1,
    allow_files=True,
    allow_folders=True,
    recursive=True
)

# Useful comments
file_two = models.FilePathField(
    path=FIELD_PATH2,
    allow_files=True,
    allow_folders=True,
    recursive=True
)

.. but that's because I'm a stickler for PEP8!) :)

..但那是因为我是PEP8的坚持者!):)

#1


4  

I agree with Pete that you definitely don't want to get too tricksy with a simple model definition. You could make the multiple nearly-the-same file fields a little easier to manage and still be explicit by keeping your defaults in a dictionary and using the ** operator. Something like:

我同意Pete的观点,你绝对不希望通过简单的模型定义过于狡猾。您可以通过将默认值保存在字典中并使用**运算符来使多个几乎相同的文件字段更容易管理并且仍然是明确的。就像是:

filefield_defaults = {
    'allow_files':True, 
    'allow_folders':True, 
    'recursive':True
}

file_one = models.FilePathField(
    path=FIELD_PATH1,
    **filefield_defaults
)

file_two = models.FilePathField(
    path=FIELD_PATH2,
    **filefield_defaults
)

#2


5  

Honestly, DRY code is important and should be strived for, but there are limits :) In this case you're at odds between DRY and line two of the zen of python Explicit is better than implicit. If I was maintaining your code I'd much rather come in and see:

老实说,DRY代码很重要,应该努力,但有限制:)在这种情况下,你在DRY和蟒蛇明确的第二行之间的差异优于隐式。如果我维护你的代码,我宁愿进来看看:

file_one = models.FilePathField(path=FIELD_PATH1, allow_files=True, allow_folders=True, recursive=True)
file_two = models.FilePathField(path=FIELD_PATH2, allow_files=True, allow_folders=True, recursive=True)
file_three = models.FilePathField(path=FIELD_PATH3, allow_files=True, allow_folders=True, recursive=True)

Because while not being "DRY", it is immediately obvious what is going on, and I don't have to waste time going "wait, what?"

因为虽然不是“干”,但是很明显发生了什么,我不必浪费时间去“等,什么?”

(Actually strictly speaking what I'd like to see is:

(实际上严格来说,我希望看到的是:

# Useful comments
file_one = models.FilePathField(
    path=FIELD_PATH1,
    allow_files=True,
    allow_folders=True,
    recursive=True
)

# Useful comments
file_two = models.FilePathField(
    path=FIELD_PATH2,
    allow_files=True,
    allow_folders=True,
    recursive=True
)

.. but that's because I'm a stickler for PEP8!) :)

..但那是因为我是PEP8的坚持者!):)