为什么Django的ModelAdmin使用元组上的列表,反之亦然 [英] Why Django's ModelAdmin uses lists over tuples and vice-versa
问题描述
从Django入门教程的 \ mysite \ polls \ admin.py
中:
From the Django intro tutorial, in \mysite\polls\admin.py
:
from django.contrib import admin
#...
class PollAdmin(admin.ModelAdmin):
#...
inlines = [ChoiceInline]
list_display = ('question', 'pub_date', 'was_published_today')
list_filter = ['pub_date']
admin.site.register(Poll, PollAdmin)
为什么内联和 list_filter
都使用列表,而 list_display
使用元组?内联和 list_filters
是否出于某种原因需要可变吗?
Why do inlines and list_filter
both use lists, while list_display
uses a tuple? Do inlines and list_filters
need to be mutable for some reason?
我只是想在这里理解设计决定.
I'm just trying to understand the design decision here.
推荐答案
使用哪个都没有关系,因为Django(和您)永远不会在运行时更改它们.重要的是值必须是字符串的可迭代值.当只有一个元素时,我经常使用 foo = ["something"]
,因为当我不小心说出 foo =("somthing")
时,我经常被钉上钉子的 foo =("something",)
.
It doesn't matter which you use because Django (and you) will never change them during runtime. All that's important is that the value be an iterable of strings. I often use foo = ["something"]
when there is only one element because I've gotten nailed so often when I accidentally say foo = ("somthing")
instead of foo = ("something",)
.
我会在重要空白"之后紧随其后的Python刺激因素列表中添加这个单元素元组"符号问题.也就是说,我仍然喜欢这种语言.
I would put this one-element-tuple-notation issue on my list of Python irritants, right after "significant whitespace". That said, I still love the language.
这篇关于为什么Django的ModelAdmin使用元组上的列表,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!