Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete method #821

Open
nandoys opened this issue Jan 16, 2023 · 5 comments
Open

delete method #821

nandoys opened this issue Jan 16, 2023 · 5 comments

Comments

@nandoys
Copy link

nandoys commented Jan 16, 2023

hello i've an issue when i call delete method on my object
RecursionError: maximum recursion depth exceeded while calling a Python object

@matthiask
Copy link
Member

Well, without a full traceback it's impossible to tell what's going on. Maybe also post the delete function code?

@nandoys
Copy link
Author

nandoys commented Jan 16, 2023

delete view on views.py
`
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 KeyError as e:
    context = {
        'message': 'une clé manquante: {}'.format(e.__str__())
    }
    return Response(context, status=status.HTTP_400_BAD_REQUEST)
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)

`

my model
`
class Plan(MPTTModel):
groups = [('actif', 'Actif'), ('passif', 'Passif'), ('charge', 'Charge'), ('produit', 'Produit')]
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')
group = models.CharField(max_length=50, choices=groups, null=True, blank=True)
entreprise = models.ForeignKey(Account, on_delete=models.CASCADE, null=True)

def __str__(self):
    return str(self.coding)

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")

def delete(self, *args, **kwargs):
    with transaction.atomic():
        super().delete()

class MPTTMeta:
    order_insertion_by = ['coding']

class Meta:
    constraints = [
        models.UniqueConstraint(fields=['coding'], condition=Q(entreprise__isnull=True), name='unique_coding'),
        models.UniqueConstraint(fields=['coding'], condition=Q(entreprise__isnull=True) & Q(parent__isnull=False),
                                name='unique_coding_with_parent'),

        models.UniqueConstraint(fields=['title'], condition=Q(entreprise__isnull=True), name='unique_title'),
        models.UniqueConstraint(fields=['title'], condition=Q(entreprise__isnull=True) & Q(parent__isnull=False),
                                name='unique_title_with_parent'),

        models.UniqueConstraint(fields=['coding', 'title'], condition=Q(entreprise__isnull=True),
                                name='unique_coding_title'),
        models.UniqueConstraint(fields=['coding', 'title'],
                                condition=Q(entreprise__isnull=True) & Q(parent__isnull=False),
                                name='unique_coding_title_with_parent'),

        models.UniqueConstraint(fields=['coding', 'entreprise'], name='unique_entreprise_code',
                                violation_error_message='Ce code existe déjà'),
        models.UniqueConstraint(fields=['title', 'entreprise'], name='unique_entreprise_title',
                                violation_error_message='Ce intitulé de compte existe déjà'),
    ]

`

@nandoys
Copy link
Author

nandoys commented Jan 16, 2023

Well, without a full traceback it's impossible to tell what's going on. Maybe also post the delete function code?

please see the below code

@matthiask
Copy link
Member

I wonder if the transaction.atomic wrapper may be problematic?

I don't know why that would lead to a recursion error. Maybe MPTTModel does something strange with the delete method but reading the mptt code doesn't contain strange cases, I don't know.

@nandoys
Copy link
Author

nandoys commented Jan 16, 2023

I wonder if the transaction.atomic wrapper may be problematic?

I don't know why that would lead to a recursion error. Maybe MPTTModel does something strange with the delete method but reading the mptt code doesn't contain strange cases, I don't know.

i've add transaction.atomic t fix the recursion issue, but it doesn't change the situation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants