Django Admin中删除操作发生奇怪的错误。我可以从控制台删除对象,但不能从管理面板删除对象。似乎与这些字段有关。
:YcmRestartServer
这是日志
class CategoryAdmin(DistributedAdminMixin, admin.ModelAdmin):
exclude = ('terminal',)
class ArticleAdmin(DistributedAdminMixin, admin.ModelAdmin):
exclude = ('terminal',)
list_display = ('title', 'is_displayed', 'category')
search_fields = ('title', 'category')
readonly_fields = ('created_by',)
admin.site.register(Category, CategoryAdmin)
admin.site.register(Article, ArticleAdmin)
您可以在上面看到的模型代码。对于外键关系,使用CASCADE一对多
File "/home/artem/Projects/carbon-delta-lab/.venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/artem/Projects/carbon-delta-lab/.venv/lib/python3.6/site-packages/django/contrib/admin/options.py", line 575, in wrapper
return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/artem/Projects/carbon-delta-lab/.venv/lib/python3.6/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/artem/Projects/carbon-delta-lab/.venv/lib/python3.6/site-packages/django/views/decorators/cache.py", line 44, in _wrapped_view_func
response = view_func(request, *args, **kwargs)
File "/home/artem/Projects/carbon-delta-lab/.venv/lib/python3.6/site-packages/django/contrib/admin/sites.py", line 223, in inner
return view(request, *args, **kwargs)
File "/home/artem/Projects/carbon-delta-lab/.venv/lib/python3.6/site-packages/django/utils/decorators.py", line 62, in _wrapper
return bound_func(*args, **kwargs)
File "/home/artem/Projects/carbon-delta-lab/.venv/lib/python3.6/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
response = view_func(request, *args, **kwargs)
File "/home/artem/Projects/carbon-delta-lab/.venv/lib/python3.6/site-packages/django/utils/decorators.py", line 58, in bound_func
return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/artem/Projects/carbon-delta-lab/.venv/lib/python3.6/site-packages/django/contrib/admin/options.py", line 1618, in changelist_view
response = self.response_action(request, queryset=cl.get_queryset(request))
File "/home/artem/Projects/carbon-delta-lab/.venv/lib/python3.6/site-packages/django/contrib/admin/options.py", line 1331, in response_action
response = func(self, request, queryset)
File "/home/artem/Projects/carbon-delta-lab/.venv/lib/python3.6/site-packages/django/contrib/admin/actions.py", line 36, in delete_selected
queryset, opts, request.user, modeladmin.admin_site, using)
File "/home/artem/Projects/carbon-delta-lab/.venv/lib/python3.6/site-packages/django/contrib/admin/utils.py", line 131, in get_deleted_objects
collector.collect(objs)
File "/home/artem/Projects/carbon-delta-lab/.venv/lib/python3.6/site-packages/django/contrib/admin/utils.py", line 195, in collect
return super().collect(objs, source_attr=source_attr, **kwargs)
File "/home/artem/Projects/carbon-delta-lab/.venv/lib/python3.6/site-packages/django/db/models/deletion.py", line 222, in collect
field.remote_field.on_delete(self, field, sub_objs, self.using)
TypeError: 'str' object is not callable
也许与数据库有关?但是似乎在对数据库进行任何查询之前都会失败
答案 0 :(得分:0)
似乎created_by
模型的category
和Article
字段具有无效的on_delete
属性:应为:
on_delete=models.SET_NULL
代替on_delete="SET_NULL"
on_delete=models.CASCADE
代替on_delete="CASCADE"