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
)
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.
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.
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.
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.
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')
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With