I've been trying to delete this model object but i keep getting this error:
Field 'id' expected a number but got 'deleted template'
I tried deleting the object from the admin side (thinking it is a problem with my view) but the error remain the same.
models
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
views
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')
EDIT
Below is the traceback
Traceback:
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'.
Any help is appreciated.
Thanks.
I managed to understand where the problem was coming from.
There's a ForeignKey
pointing to TransactionalTemplate
model with on_delete=models.SET_DEFAULT
and default='deleted template'
. it was defined as below:
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'))
when deleting a TransactionalTemplate
object that is related to another object in Inbox
model django tries to maintain the related object by setting it's value to whatever is set as the value of default
, it expects a number (int)
to be the default but all it could found was the string 'deleted template'
therefore raising ValueError
.
so all i did to get rid of the error was:
on_delete=models.SET_NULL
to on_delete=models.CASCADE
default='deleted template'
./manage.py makemigrations
and migrate