Can't delete records in ManyToMany field?

Is the data model "Task Group" field with a ManyToMany field 'comments'.

How to delete all records in the field 'comments' of a particular object? Tried code below, but it brought an error. What did I do wrong?

models.py:
class GroupTask(models.Model):
 code = models.UUIDField(_('Code'), primary_key=True, default=uuid.uuid4, editable=False)
 comments = models.ManyToManyField (Comment)


views.py:
def group_task_edit(request, group_task_code):
 GroupTask.comments.through.objects.filter(pk=group_task_code).delete()


ERROR:
Traceback (most recent call last):
 File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\exception.py", line 39, in inner
 response = get_response(request)
 File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
 response = self.process_exception_by_middleware(e, request)
 File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
 response = wrapped_callback(request, *callback_args, **callback_kwargs)
 File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\reversion\revisions.py", line 296, in do_revision_context
 return func(*args, **kwargs)
 File "C:\Users\Nurzhan\PycharmProjects\RMS\project\views.py", line 124, in group_task_edit
GroupTask.comments.through.objects.filter(pk=group_task_code).delete()
 File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\manager.py", line 85, in manager_method
 return getattr(self.get_queryset(), name)(*args, **kwargs)
 File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\query.py", line 796, in filter
 return self._filter_or_exclude(False, *args, **kwargs)
 File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\query.py", line 814, in _filter_or_exclude
 clone.query.add_q(Q(*args, **kwargs))
 File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\sql\query.py", line 1227, in add_q
 clause, _ = self._add_q(q_object, self.used_aliases)
 File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\sql\query.py", line 1253, in _add_q
 allow_joins=allow_joins, split_subq=split_subq,
 File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\sql\query.py", line 1187, in build_filter
 condition = self.build_lookup(lookups, col, value)
 File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\sql\query.py", line 1083, in build_lookup
 return final_lookup(lhs, rhs)
 File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\lookups.py", line 19, in __init__
 self.rhs = self.get_prep_lookup()
 File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\lookups.py", line 59, in get_prep_lookup
 return self.lhs.output_field.get_prep_value(self.rhs)
 File "C:\Users\Nurzhan\AppData\Local\Programs\Python\Python35-32\lib\site-packages\django\db\models\fields\__init__.py", line 946, in get_prep_value
 return int(value)
ValueError: invalid literal for int() with base 10: '98267cbf-7cb5-47a6-9e44-fd3e86ca3520'
June 27th 19 at 15:28
1 answer
June 27th 19 at 15:30
Solution
task = GroupTask.objects.first() # or what you want to get
task.comments.clear()

Find more questions by tags DjangoPython