每次我尝试删除特定模型的实例时,都会收到错误:
object() takes no parameters
我查找了此错误消息,但所有帖子都指的是错误的 init 设置,我没有。
这让我疯了!知道是什么导致了这个吗?这是模型:
class CharacterSheet(models.Model):
objects = ModelManager()
save_name = models.CharField(max_length=150, blank=True, null=True)
character_name = models.CharField(max_length=150, blank=True, null=True)
root = models.TextField(
default="{'lastId':'0', 'lastPlayerAddedId':'0', 'is_root':'true', 'type': 'section', 'direction': 'vertical'}")
created = models.DateField(auto_now_add=True)
edited = models.DateField(auto_now=True)
color = models.IntegerField(default=random_color)
template = models.ForeignKey('self', related_name="copies", on_delete=models.Empty, null=True)
saved_on_profile = models.ForeignKey(Profile, on_delete=models.CASCADE, null=True)
@property
def root_json(self):
return json.loads(self.root)
def save(self, *args, **kwargs):
"""Save method override"""
super(CharacterSheet, self).save(*args, **kwargs)
for character in self.characters.all():
character.save()
完整错误堆栈:
File "/home/vvdrrltv/virtualenv/rolegate.com_rolegate__django/3.5/lib/python3.5/site-packages/django/core/handlers/exception.py" in inner
35. response = get_response(request)
File "/home/vvdrrltv/virtualenv/rolegate.com_rolegate__django/3.5/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
128. response = self.process_exception_by_middleware(e, request)
File "/home/vvdrrltv/virtualenv/rolegate.com_rolegate__django/3.5/lib/python3.5/site-packages/django/core/handlers/base.py" in _get_response
126. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/vvdrrltv/virtualenv/rolegate.com_rolegate__django/3.5/lib/python3.5/site-packages/django/contrib/admin/options.py" in wrapper
574. return self.admin_site.admin_view(view)(*args, **kwargs)
File "/home/vvdrrltv/virtualenv/rolegate.com_rolegate__django/3.5/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)
File "/home/vvdrrltv/virtualenv/rolegate.com_rolegate__django/3.5/lib/python3.5/site-packages/django/views/decorators/cache.py" in _wrapped_view_func
44. response = view_func(request, *args, **kwargs)
File "/home/vvdrrltv/virtualenv/rolegate.com_rolegate__django/3.5/lib/python3.5/site-packages/django/contrib/admin/sites.py" in inner
223. return view(request, *args, **kwargs)
File "/home/vvdrrltv/virtualenv/rolegate.com_rolegate__django/3.5/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapper
62. return bound_func(*args, **kwargs)
File "/home/vvdrrltv/virtualenv/rolegate.com_rolegate__django/3.5/lib/python3.5/site-packages/django/utils/decorators.py" in _wrapped_view
142. response = view_func(request, *args, **kwargs)
File "/home/vvdrrltv/virtualenv/rolegate.com_rolegate__django/3.5/lib/python3.5/site-packages/django/utils/decorators.py" in bound_func
58. return func.__get__(self, type(self))(*args2, **kwargs2)
File "/home/vvdrrltv/virtualenv/rolegate.com_rolegate__django/3.5/lib/python3.5/site-packages/django/contrib/admin/options.py" in delete_view
1713. return self._delete_view(request, object_id, extra_context)
File "/home/vvdrrltv/virtualenv/rolegate.com_rolegate__django/3.5/lib/python3.5/site-packages/django/contrib/admin/options.py" in _delete_view
1737. [obj], opts, request.user, self.admin_site, using)
File "/home/vvdrrltv/virtualenv/rolegate.com_rolegate__django/3.5/lib/python3.5/site-packages/django/contrib/admin/utils.py" in get_deleted_objects
131. collector.collect(objs)
File "/home/vvdrrltv/virtualenv/rolegate.com_rolegate__django/3.5/lib/python3.5/site-packages/django/contrib/admin/utils.py" in collect
195. return super().collect(objs, source_attr=source_attr, **kwargs)
File "/home/vvdrrltv/virtualenv/rolegate.com_rolegate__django/3.5/lib/python3.5/site-packages/django/db/models/deletion.py" in collect
222. field.remote_field.on_delete(self, field, sub_objs, self.using)
Exception Type: TypeError at /admin/playerdata/charactersheet/1240/delete/
Exception Value: object() takes no parameters
答案 0 :(得分:2)
你的问题在这里:
template = models.ForeignKey(
'self', related_name="copies",
on_delete=models.Empty, null=True)
on_delete
值无效;你需要选择one of the valid options。
例如,如果您要将引用替换为models.Empty
,则必须改为使用models.SET(models.Empty)
。
答案 1 :(得分:1)
看看Django doc for ForeignKey.on_delete。可能的值有:
您在字段models.Empty
中使用的值(template
)无效,可能是导致错误的原因