Django: can't delete item in Model inherting django-mptt

in models.py, i've the following code:

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']

and in my views.py, i've this code:

@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)`

when i go to the related url attached to that view, i got an error with this message:

Internal Server Error: /apis/accounting/plan/delete

Traceback (most recent call last):

During handling of the above exception, another exception occurred:

Traceback (most 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: maximum recursion depth exceeded while calling a Python object

\[17/Jan/2023 00:51:44\] "DELETE /apis/accounting/plan/delete HTTP/1.1" 500 3592846

please, how can i solve this ?

i expect to delete item from my mptt model when i send request to the related url

Back to Top