Django: невозможно удалить элемент в модели, наследуя django-mptt
в models.py у меня есть следующий код:
import uuidfrom django.db import modelsfrom django.db.models import Qfrom mptt.models import MPTTModel, TreeForeignKey
from company.models import Account
class Plan(MPTTModel):
id = models.UUIDField(primary_key=True, unique=True, editable=False,
default=uuid.uuid4)
coding = models.CharField(max_length=50)
title = models.CharField(max_length=100)
parent = TreeForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, related_name='children')
entreprise = models.ForeignKey(Account, on_delete=models.CASCADE, null=True)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.parent is not None:
coding = str(self.coding)
parent = str(self.parent)
if not coding.startswith(parent):
raise ValueError('Le code ne correspond pas au code parent {}'.format(parent))
if len(coding) == len(parent):
raise ValueError("Le code ne peut pas être identique au code parent {}".format(parent))
if self.parent.get_level() == 0 and self.entreprise is not None:
raise ValueError("Vous ne pouvez pas associer une entreprise à ce niveau")
else:
if self.entreprise is not None:
raise ValueError("Vous ne pouvez pas associer une entreprise à ce niveau")
class MPTTMeta:
order_insertion_by = ['coding']
и в моем views.py, у меня есть этот код:
@api_view(['DELETE'])
def delete_plan(request):
try:
id = request.data['id']
entreprise = request.data\['entreprise'\]
plan = Plan.objects.get(id=id, entreprise=entreprise)
context = {
'message': 'Le compte {} - {} a été supprimé de votre plan comptable avec succès'.format(plan.coding,
plan.title)
}
plan.delete()
return Response(context, status=status.HTTP_200_OK)
except Plan.DoesNotExist as e:
context = {
'message': "Ce compte n'existe pas dans votre plan comptable"
}
return Response(context, status=status.HTTP_404_NOT_FOUND)`
Когда я перехожу к связанному url, прикрепленному к этому представлению, я получаю ошибку с таким сообщением:
Внутренняя ошибка сервера: /apis/accounting/plan/delete
Traceback (last recent call last):
Во время обработки вышеуказанного исключения произошло другое исключение:
Traceback (last recent call last):
File "/Users/nandoys/Documents/trust_compta/venv/lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 186, in _get_
rel_obj = self.field.get_cached_value(instance)
File "/Users/nandoys/Documents/trust_compta/venv/lib/python3.8/site-packages/django/db/models/fields/mixins.py", line 15, in get_cached_value
return instance.\_state.fields_cache\[cache_name\]
KeyError: 'parent'
...
File "\<string\>", line 1, in _new_
RecursionError: превышена максимальная глубина рекурсии при вызове объекта Python
\[17/Jan/2023 00:51:44\] "DELETE /apis/accounting/plan/delete HTTP/1.1" 500 3592846
пожалуйста, как я могу решить это?
я ожидаю удаления элемента из моей mptt модели, когда я отправляю запрос на связанный url