Как удалить отношения "многие ко многим" в django?
Я пытаюсь удалить мое m2m отношение, я думаю, что я написал его правильно, но в DB ничего не происходит, и на сайте тоже ничего не происходит
вот мой models.py
class Department(models.Model):
id = models.AutoField(primary_key=True)
department = models.CharField(max_length=60)
info_grafana = models.TextField()
users = models.ManyToManyField(User)
и мой views.py
class ViewUserAccount(View):
def post(self, request, id_account: int, *args, **kwargs):
if request.POST.get('_method') == 'delete':
self.delete(request, id_account)
else:
self.create(request, id_account)
return redirect(request.META.get('HTTP_REFERER'))
def delete(self, request, id_account):
id_direction = request.POST.get('direction_id')
id_project = request.POST.get('project_id')
if id_project:
project = Project.objects.get(id=id_project)
project.users.remove(User.objects.get(id=id_account))
elif id_direction:
direction = Department.objects.get(id=id_direction)
direction.users.remove(User.objects.get(id=id_account))
главное, что я хочу удалить пользователя из отдела/проекта, но ничего не происходит :(
)вот что я получаю от frontend
<QueryDict: {'csrfmiddlewaretoken': ['Fa3C0uuP4dlsrvSr1zkvD9UiovRnid7JYYRPRSksNsm0cLXCFPs4ROT0k2M7nv2E'], '_method': ['delete'], 'id_project': ['1'], 'id_direction': ['68']}>
Похоже, что у вас опечатка при попытке получить параметры (фронтенд отправляет id_project и id_direction, а вы пытаетесь получить project_id и direction_id). Чтобы решить эту проблему, просто измените:
def delete(self, request, id_account):
id_direction = request.POST.get('id_direction')
id_project = request.POST.get('id_project')
...