ValueError: Field 'id' expected a number but got 'deleted "" ' - Django Generic DeleteView
ValueError: Field 'id' expected a number but got 'deleted "" ' - Django Generic DeleteView
我一直在尝试删除这个模型对象,但我一直收到这个错误:
Field 'id' expected a number but got 'deleted template'
我尝试从管理员端删除该对象(认为这是我的观点的问题)但错误仍然存在。
型号
class TransactionalTemplate(models.Model):
name = models.CharField(_('name'), max_length=30)
role = models.ForeignKey(Group, on_delete=models.CASCADE, verbose_name=_('role'))
content = models.TextField(_('body'))
category= models.CharField(_('category'), max_length=10, choices=CATEGORIES)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('mail_and_sms:template_list')
def get_role(self):
return self.role
观看次数
class TemplateDeleteView(DeleteView):
model = TransactionalTemplate
template_name = 'template_confirm_delete.html'
success_message = _('was sucessfully deleted')
def delete(self, request, *args, **kwargs):
messages.success(request, f'{self.get_object()} {self.success_message}')
self.get_object().delete()
return redirect('mail_and_sms:template_list')
编辑
下面是回溯
回溯:
Traceback (most recent call last):
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 1772, in get_prep_value
return int(value)
The above exception (invalid literal for int() with base 10: 'deleted template') was the direct cause of the following exception:
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/abdoul/Desktop/DevWork/c/mail_and_sms/views.py", line 75, in dispatch
return super(TemplateDeleteView, self).dispatch(request, *args, **kwargs)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/views/generic/base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/views/generic/edit.py", line 218, in post
return self.delete(request, *args, **kwargs)
File "/home/abdoul/Desktop/DevWork/c/mail_and_sms/views.py", line 67, in delete
self.get_object().delete()
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/base.py", line 938, in delete
return collector.delete()
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/deletion.py", line 322, in delete
{field.name: value}, self.using)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/sql/subqueries.py", line 107, in update_batch
self.get_compiler(using).execute_sql(NO_RESULTS)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1492, in execute_sql
cursor = super().execute_sql(result_type)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1131, in execute_sql
sql, params = self.as_sql()
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1458, in as_sql
val = field.get_db_prep_save(val, connection=self.connection)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/fields/related.py", line 939, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 821, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 2365, in get_db_prep_value
value = self.get_prep_value(value)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 1776, in get_prep_value
) from e
Exception Type: ValueError at /app/mailandsms/template/delete/9/
Exception Value: Field 'id' expected a number but got 'deleted template'.
感谢任何帮助。
谢谢。
我设法了解问题出在哪里。
有一个 ForeignKey
指向带有 on_delete=models.SET_DEFAULT
和 default='deleted template'
的 TransactionalTemplate
模型。它被定义如下:
class Outbox(models.Model):
template = models.ForeignKey(TransactionalTemplate,
on_delete=models.SET_DEFAULT, default='deleted template', verbose_name=_('template')) ---- > here
recipients = models.ManyToManyField(settings.AUTH_USER_MODEL, verbose_name=_('recipients'))
timestamp = models.DateTimeField(auto_now_add=True)
subject = models.CharField(_('subject'), max_length=255)
message = models.TextField(_('message'))
删除与 Inbox
模型中另一个对象相关的 TransactionalTemplate
对象时,django 会尝试通过将其值设置为 [=17= 的值来维护相关对象],它期望数字 (int)
是默认值,但它所能找到的只是字符串 'deleted template'
,因此引发 ValueError
.
所以我为消除错误所做的一切是:
- 将
on_delete=models.SET_NULL
更改为on_delete=models.CASCADE
- 移除
default='deleted template'
./manage.py makemigrations
和 migrate
- 最后编辑迁移文件以将初始默认值更改为数字
已解决
根据您的错误:
Exception Value: Field 'id' expected a number but got 'deleted template'.
您必须在视图中为 id 参数发送数字,但您发送了错误的字符串值 "deleted template"
更改您的输入值。并 再次测试。
我一直在尝试删除这个模型对象,但我一直收到这个错误:
Field 'id' expected a number but got 'deleted template'
我尝试从管理员端删除该对象(认为这是我的观点的问题)但错误仍然存在。
型号
class TransactionalTemplate(models.Model):
name = models.CharField(_('name'), max_length=30)
role = models.ForeignKey(Group, on_delete=models.CASCADE, verbose_name=_('role'))
content = models.TextField(_('body'))
category= models.CharField(_('category'), max_length=10, choices=CATEGORIES)
def __str__(self):
return self.name
def get_absolute_url(self):
return reverse('mail_and_sms:template_list')
def get_role(self):
return self.role
观看次数
class TemplateDeleteView(DeleteView):
model = TransactionalTemplate
template_name = 'template_confirm_delete.html'
success_message = _('was sucessfully deleted')
def delete(self, request, *args, **kwargs):
messages.success(request, f'{self.get_object()} {self.success_message}')
self.get_object().delete()
return redirect('mail_and_sms:template_list')
编辑
下面是回溯
回溯:
Traceback (most recent call last):
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 1772, in get_prep_value
return int(value)
The above exception (invalid literal for int() with base 10: 'deleted template') was the direct cause of the following exception:
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/core/handlers/base.py", line 115, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/core/handlers/base.py", line 113, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/views/generic/base.py", line 71, in view
return self.dispatch(request, *args, **kwargs)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/utils/decorators.py", line 43, in _wrapper
return bound_method(*args, **kwargs)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/contrib/auth/decorators.py", line 21, in _wrapped_view
return view_func(request, *args, **kwargs)
File "/home/abdoul/Desktop/DevWork/c/mail_and_sms/views.py", line 75, in dispatch
return super(TemplateDeleteView, self).dispatch(request, *args, **kwargs)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/views/generic/base.py", line 97, in dispatch
return handler(request, *args, **kwargs)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/views/generic/edit.py", line 218, in post
return self.delete(request, *args, **kwargs)
File "/home/abdoul/Desktop/DevWork/c/mail_and_sms/views.py", line 67, in delete
self.get_object().delete()
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/base.py", line 938, in delete
return collector.delete()
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/deletion.py", line 322, in delete
{field.name: value}, self.using)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/sql/subqueries.py", line 107, in update_batch
self.get_compiler(using).execute_sql(NO_RESULTS)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1492, in execute_sql
cursor = super().execute_sql(result_type)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1131, in execute_sql
sql, params = self.as_sql()
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/sql/compiler.py", line 1458, in as_sql
val = field.get_db_prep_save(val, connection=self.connection)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/fields/related.py", line 939, in get_db_prep_save
return self.target_field.get_db_prep_save(value, connection=connection)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 821, in get_db_prep_save
return self.get_db_prep_value(value, connection=connection, prepared=False)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 2365, in get_db_prep_value
value = self.get_prep_value(value)
File "/home/abdoul/Desktop/DevWork/c_env/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 1776, in get_prep_value
) from e
Exception Type: ValueError at /app/mailandsms/template/delete/9/
Exception Value: Field 'id' expected a number but got 'deleted template'.
感谢任何帮助。
谢谢。
我设法了解问题出在哪里。
有一个 ForeignKey
指向带有 on_delete=models.SET_DEFAULT
和 default='deleted template'
的 TransactionalTemplate
模型。它被定义如下:
class Outbox(models.Model):
template = models.ForeignKey(TransactionalTemplate,
on_delete=models.SET_DEFAULT, default='deleted template', verbose_name=_('template')) ---- > here
recipients = models.ManyToManyField(settings.AUTH_USER_MODEL, verbose_name=_('recipients'))
timestamp = models.DateTimeField(auto_now_add=True)
subject = models.CharField(_('subject'), max_length=255)
message = models.TextField(_('message'))
删除与 Inbox
模型中另一个对象相关的 TransactionalTemplate
对象时,django 会尝试通过将其值设置为 [=17= 的值来维护相关对象],它期望数字 (int)
是默认值,但它所能找到的只是字符串 'deleted template'
,因此引发 ValueError
.
所以我为消除错误所做的一切是:
- 将
on_delete=models.SET_NULL
更改为on_delete=models.CASCADE
- 移除
default='deleted template'
./manage.py makemigrations
和migrate
- 最后编辑迁移文件以将初始默认值更改为数字
已解决 根据您的错误:
Exception Value: Field 'id' expected a number but got 'deleted template'.
您必须在视图中为 id 参数发送数字,但您发送了错误的字符串值 "deleted template"
更改您的输入值。并 再次测试。