Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is this not a tuple?

I can't figure out what I'm doing wrong here. My error is: ImproperlyConfigured at /admin/ 'CategoryAdmin.fields' must be a list or tuple.

Isn't the CategoryAdmin.fields a tuple? Am I reading this wrong?

admin.py ..

class CategoryAdmin(admin.ModelAdmin):
    fields = ('title')
    list_display = ('id', 'title', 'creation_date')

class PostAdmin(admin.ModelAdmin):
    fields = ('author', 'title', 'content')
    list_display = ('id', 'title', 'creation_date')

admin.site.register(
    models.Category, 
    CategoryAdmin
)
admin.site.register(
    models.Post, 
    PostAdmin
)
like image 785
JREAM Avatar asked Mar 14 '13 14:03

JREAM


People also ask

How do you check if it is a tuple or not?

Use isinstance() to check the data type of a variable in Python. Use isinstance(var, class) with var as the variable to compare and class as either list or tuple to determine if obj is a list or a tuple. isinstance(var, class) returns True if var is of type class and False otherwise.

How do you check if something is not a tuple in Python?

To check if any item of a Tuple is True in Python, call any() builtin function and pass the tuple object as argument to it. any() returns True if any the item of Tuple is True, or else it returns False.

Is it a tuple or a list?

Immutable Tuples. We have heard already that tuples are immutable while lists are mutable. It simply means that you can change the existing values in a list. But you cannot change the same value if it is stored in the tuple.

Which is an example of tuple?

Tuple. Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable.


1 Answers

No, it is not. You need to add a comma:

fields = ('title',)

It is the comma that makes this a tuple. The parenthesis are really just optional here:

>>> ('title')
'title'
>>> 'title',
('title',)

The parenthesis are of course still a good idea, with parenthesis tuples are easier to spot visually, and the parenthesis distinguish the tuple in a function call from other parameters (foo(('title',), 'bar') is different from foo('title', 'bar')).

like image 108
Martijn Pieters Avatar answered Oct 18 '22 20:10

Martijn Pieters