问题
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
)
回答1:
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')
).
回答2:
It should be:
fields = ('title', )
Example:
In [64]: type(('title'))
Out[64]: str
In [65]: type(('title', ))
Out[65]: tuple
回答3:
You need a comma after title:
fields = ('title',)
回答4:
Replace it with this:
fields = ('title', )
来源:https://stackoverflow.com/questions/15412055/is-this-not-a-tuple