3

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)

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.

miku
  • 181,842
  • 47
  • 306
  • 310
Mike M. Lin
  • 9,992
  • 12
  • 53
  • 62

1 Answers1

8

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",).

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.

Peter Rowell
  • 17,605
  • 2
  • 49
  • 65
  • Verified. Either a list or a tuple will do here -- it worked both ways. And if you try some other data type, you'll get an error similar to this: 'PollAdmin.list_filter' must be a list or tuple. – Mike M. Lin Sep 09 '10 at 16:04
  • I'm going for lists. It seems to me that tuples should be used for data that has a natural structure (which is why they are immutable.. kinda). Seems wrong to me to use tuples just to get an immutable list :o See this answer on tuples vs lists http://stackoverflow.com/questions/626759/whats-the-difference-between-list-and-tuples – pghalliday Aug 31 '16 at 18:08
  • 1
    I just read the link from @pghalliday, and saw that tuples and lists have different meanings (besides immutable). My understanding is, what'll goes into `inlines` are custom inline classes, and what'll goes into `list_filter` are "fields defined in list_display". Since the values are all "same thing", they are lists. However, `list_display` can contains (a) model's fields (b) admin's fields (i.e. extra fields defined inside your custom admin class). They are different, for example, model's fields are sortable but admin's custom fields cannot (you will notice it on the admin page). – John Pang Oct 31 '18 at 15:27
  • https://www.afternerd.com/blog/difference-between-list-tuple/ Here is a long article discuss about the performance of list and tuple. Since those fields are 'read-only' in admin, it seems better to use tuple over list, even there is one object. So, I checked `django.contrib.auth.admin.py` and found that it always use tuples. – John Pang May 22 '19 at 13:08
  • @JohnPang: I know the performance difference. This has more to do with an aging brain (now 8.5 years older since my original post) that has 2 *major* issues with Python: 'meaningful whitespace', and single-element tuple syntax. This problem exists to this day, ref [Python 3.7 docs](https://docs.python.org/3.7/reference/datamodel.html#the-standard-type-hierarchy). – Peter Rowell May 22 '19 at 18:59