Is this not a tuple?

馋奶兔 提交于 2019-12-10 13:16:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!