N
N
nurzhannogerbek2017-04-07 15:47:16
Django
nurzhannogerbek, 2017-04-07 15:47:16

Can't delete entries in a ManyToMany field?

There is a "Group Task" data model with a ManyToMany field 'comments' .
What is the correct way to delete all entries in the 'comments' field of a particular object? Tried the code below but it throws an error. What did you 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'

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2017-04-07
@nurzhannogerbek

task = GroupTask.objects.first() # Ну или какой вам там надо
task.comments.clear()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question