i'm trying to delete my m2m relation, i think i wrote it right, but in the DB nothing happens, and there is nothing happening on the website too
here is my 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)
and my 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))
the main point that i want to delete user from department/project, but nothing is happening :(
that is what i get from frontend
<QueryDict: {'csrfmiddlewaretoken': ['Fa3C0uuP4dlsrvSr1zkvD9UiovRnid7JYYRPRSksNsm0cLXCFPs4ROT0k2M7nv2E'], '_method': ['delete'], 'id_project': ['1'], 'id_direction': ['68']}>
CodePudding user response:
It seems that you have a typo while trying to get your parameters (frontend is sending id_project
and id_direction
but you're trying to get project_id
and direction_id
). To solve it just change:
def delete(self, request, id_account):
id_direction = request.POST.get('id_direction')
id_project = request.POST.get('id_project')
...