简体   繁体   English

为什么Django的ModelAdmin使用列表而不是元组,反之亦然

[英]Why Django's ModelAdmin uses lists over tuples and vice-versa

From the Django intro tutorial, in \\mysite\\polls\\admin.py : 从Django简介教程,在\\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)

Why do inlines and list_filter both use lists, while list_display uses a tuple? 为什么inlines和list_filter都使用列表,而list_display使用元组? Do inlines and list_filters need to be mutable for some reason? 因为某种原因,内联和list_filters是否需要是可变的?

I'm just trying to understand the design decision here. 我只想在这里理解设计决定。

It doesn't matter which you use because Django (and you) will never change them during runtime. 你使用哪个并不重要,因为Django(和你)在运行时永远不会改变它们。 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",) . 当我只有一个元素时,我常常使用foo = ["something"] ,因为当我不小心说foo = ("somthing")而不是foo = ("something",)时,我经常被钉死。

I would put this one-element-tuple-notation issue on my list of Python irritants, right after "significant whitespace". 我会把这个单元素元组符号问题放在我的Python刺激物列表中,就在“重要的空白”之后。 That said, I still love the language. 那就是说,我仍然喜欢这门语言。

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

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