Note that there are some explanatory texts on larger screens.

plurals
  1. POoverwritten save method endless loop?
    text
    copied!<p>the class:</p> <pre><code>class Operation(models.Model): related_operation = models.ForeignKey('self', null = True) __related_operation = None def __init__(self, *args, **kwargs): super(Operation, self).__init__(*args, **kwargs) self.__related_operation = self.related_operation def save(self, force_insert=False, force_update=False): if self.related_operation != self.__related_operation: del self.__related_operation super(Operation, self).save(force_insert, force_update) self.__related_operation = self.related_operation </code></pre> <p>the code:</p> <pre><code> (...) OperationFormSet = modelformset_factory(Operation, form=OperationCategoryOnlyForm) if request.method == "POST": formset = OperationFormSet(request.POST, queryset=Operation.objects.filter(category=category_id)) if formset.is_valid(): instances = formset.save(commit = False) for instance in instances: if instance.related_account is not None: related_operation = Operation() related_operation.related_operation = instance related_operation.save() instance.related_operation = related_operation instance.save() formset = OperationFormSet(queryset=Operation.objects.filter(category=category_id)) else: formset = OperationFormSet(queryset=Operation.objects.filter(category=category_id)) </code></pre> <p>and the problem:</p> <p>without the overwriting the method save everything works properly, but if i change related_operation in form, it of course makes second, third and so on related operation.</p> <p>so i decided to overwrite the save method - and add the __related_operation. During saving it checks if related_operation changes - if so - it should delete the old related_operation and than save (with creating a completely new one related_operation)... And it almost works! (the new related_operation is added to database, but it loops after save. It never ends. I don't know what it is doing. I have to restart apache to get any answer from server. Maybe it's stupid mistyping or something very complex - i'm stacked...</p> <p>(of course the operation class has more than one field e.g. related_account, so the if instance.related_account is not None: is correct)</p> <p><strong>UPDATE:</strong></p> <p>I found, that the problem is here:</p> <pre><code>def __init__(self, *args, **kwargs): super(Operation, self).__init__(*args, **kwargs) #self.__related_operation = self.related_operation </code></pre> <p>If i comment out the line above - everything goes ok - but of course it is wrong - why it blocks sth.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload