简体   繁体   English

这不是一个元组吗?

[英]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. 我的错误是:在ImproperlyConfigured /管理/“CategoryAdmin.fields”必须是一个列表或元组。

Isn't the CategoryAdmin.fields a tuple? CategoryAdmin.fields不是一个元组吗? Am I reading this wrong? 我读错了吗?

admin.py .. 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
)

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') ). 括号当然仍然是个好主意,括号元组更容易在视觉上看到,括号将函数调用中的元组与其他参数区分开( foo(('title',), 'bar')foo('title', 'bar')不同foo('title', 'bar') )。

It should be: 它应该是:

fields = ('title', )

Example: 例:

In [64]: type(('title'))
Out[64]: str

In [65]: type(('title', ))
Out[65]: tuple

替换为:

fields = ('title', )

标题后需要逗号:

fields = ('title',)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM