django的新手。我试图修改模型以在同一行上获得2个字段。阅读关于https://docs.djangoproject.com/en/2.1/ref/contrib/admin/的文档之后 我在admin.py中尝试了以下操作:
from django.contrib import admin
from .models import *
# Register your models here.
class NewsAdd(admin.ModelAdmin):
fieldsets = [
(None, {
'fields': ('field1'('field2','field3'),'field4','field5','field6','field7')})]
class NewsDisplay(admin.ModelAdmin):
list_display = ('nick','hosty','msg','info','specjalista','od','do')
search_fields = ('nick','hosty','msg','info','specjalista','od','do')
admin.site.register(News, NewsDisplay, NewsAdd)
请不要介意admin.site.register中的News和NewsDisplay,因为它们可以正常工作。
将NewsAdd添加到admin.site.register()后,立即出现以下错误:
Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x00000000044C5E18>
Traceback (most recent call last):
File "C:\Python36\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "C:\Python36\lib\site-packages\django\core\management\commands\runserver.py", line 109, in inner_run
autoreload.raise_last_exception()
File "C:\Python36\lib\site-packages\django\utils\autoreload.py", line 248, in raise_last_exception
raise _exception[1]
File "C:\Python36\lib\site-packages\django\core\management\__init__.py", line 337, in execute
autoreload.check_errors(django.setup)()
File "C:\Python36\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
fn(*args, **kwargs)
File "C:\Python36\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Python36\lib\site-packages\django\apps\registry.py", line 120, in populate
app_config.ready()
File "C:\Python36\lib\site-packages\django\contrib\admin\apps.py", line 24, in ready
self.module.autodiscover()
File "C:\Python36\lib\site-packages\django\contrib\admin\__init__.py", line 26, in autodiscover
autodiscover_modules('admin', register_to=site)
File "C:\Python36\lib\site-packages\django\utils\module_loading.py", line 47, in autodiscover_modules
import_module('%s.%s' % (app_config.name, module_to_search))
File "C:\Python36\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 678, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "C:\Users\ji34ix\PycharmProjects\Diego\src\Hosting\Wmit_news\admin.py", line 21, in <module>
admin.site.register(News, NewsDisplay, NewsAdd)
TypeError: register() takes from 2 to 3 positional arguments but 4 were given
我敢肯定,我在这里遗漏了一些非常简单的东西,但是不能靠我自己解决。有人可以帮忙吗?
答案 0 :(得分:1)
这应该是单个ModelAdmin
类。
admin.site.register
有两个参数:Model
类和适当的ModelAdmin
类。您正在尝试传递三个非法的参数。
list_display,search_fields和fieldsets可以合并为一个类定义。
class NewsAdmin(admin.ModelAdmin):
fieldsets = [
(None, {
'fields': ('field1'('field2','field3'),'field4','field5','field6','field7')})]
list_display = ('nick','hosty','msg','info','specjalista','od','do')
search_fields = ('nick','hosty','msg','info','specjalista','od','do')
admin.site.register(News, NewsAdmin)