Sheraram_Prajapat
Sheraram_Prajapat

Reputation: 597

admin site list vs tuple django

when customizing admin site we can use list or tuple like

@admin.register(Model_name)
class Model_nameAdmin(admin.ModelAdmin):
  list_display = ['a', 'b', 'c']  # using list
  # lets say a, b, c are fields of model(Model_name)
                    or 
  list_display = ('a', 'b', 'c')  # using tuple 

I'm curious Is there any difference using a list over tuple or vice-versa like performance while retrieving the admin site or something like that. I know tuples are better than lists for performance but we aren't looping here. I've looked at whats-the-difference-between-lists-and-tuples, why-does-django-use-tuples-for-settings-and-not-lists, why-djangos-modeladmin-uses-lists-over-tuples-and-vice-versa, but didn't get my answer. Is it better to use tuple all the time? I'm asking specifically for the admin site.

Upvotes: 3

Views: 576

Answers (2)

tim-mccurrach
tim-mccurrach

Reputation: 6825

For all practical intents and purposes, this is going to make absolutely no difference at all.

There may be some marginal differences in performance, but any differences in speed in this case would be so incredibly small, that it would never make any difference. There would be other things that would make a much larger difference to performance. If there was an application where performance was important on the kind of scale we are talking about, there is no way you would even be using django-admin for a performance critical part of it anyway. (It is not designed to be a user-facing production ready site).

However...

There is a good reason to use tuples, and not lists though. Convention!! It's what the official django docs / and everyone else does. It might not seem very important, but if all the code looks the same it makes it more readable.

I can see that you could argue that using a list instead of a tuple wouldn't affect readability. However, if I was reading some django-admin code and saw lists being used, I'd probably stop and think for a bit about why lists are being used and not tuples. It would just stand out to me, and I'd wonder why it had been done. Eventually I'd realise there was no good reason, and that time would have been wasted.

Similar to why you might use, a style guide (such as PEP8) or formatter (such as black) to make sure everyone's code looks the same. It just makes the code a little nicer.

Upvotes: 7

Alex Nathanail
Alex Nathanail

Reputation: 52

In admin you should use tuples

The difference between a tuple and a list is that you cannot edit a tuple - it is immutable. For this reason, they are faster for python to create and they use less memory

As you will not be editing the values of those properties you should use tuples

Upvotes: 0

Related Questions