How can I avoid recursion during saving of django model

user3214546

I have a model Entry

Now whenever Entry is created for any date. Then before saving I need to make check and add more entries, for example e.g.

Entry 1 - red

Now suppose if any entry.color = red Then I also need to make two more entries like

Entry 2 - red2
Entry 3 - red3

Now I have put this in pre_save signal

@receiver(pre_save, sender=Entry)
def new_entries(sender, instance, *args, **kwargs):
     pass

Now my problem is this ends in infinite recursion as those new entries which I want to save also send pre_save signal and this loop never finishes.

Where do put this logic so that recursion does not occur?

Sebastian Wozny

I would solve it with an additional argument to save, as opposed to attaching to pre_save:

import django.db.models
class Entry(models.Model):
    color=models.CharField(max_length="50")
    def save(self,create_children=True,**kwargs):
        if create_children and not self.pk:
            result = super(Entry,self).save(**kwargs)
            Entry(color=self.color).save(create_children=False)
            Entry(color=self.color).save(create_children=False)
        else:
            result = super(Entry,self).save(**kwargs)
        return result

You want to make sure you call super(Entry,self).save(**kwargs) before you actually construct the children, otherwise you will clutter the database with models when super() raises an IntegrityError

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How can I avoid ProgrammingError: can't adapt type 'DateTimeRangeField' when saving a Django model instance to a remote database?

From Dev

How do I avoid saving duplicate data? [Django]

From Dev

How can I access a field during migration which is not in django model but available in databse

From Dev

How can I avoid events firing during InitializeComponent?

From Dev

Django conditional queries: How can I avoid this?

From Dev

How to avoid instant saving of the model with ajax validation?

From Dev

Can I avoid template recursion here?

From Dev

Saving path during tree recursion

From Dev

Recursion: how to avoid Python set changed set during iteration RuntimeError

From Dev

How can I avoid Slick's model declaration verbosity and repetitiveness

From Dev

Django - how can i call a model as a string?

From Dev

How can I model this behavior in Django?

From Dev

How can i stop this recursion?

From Dev

How can I avoid deadlocking when registering/deregistering observers during notification?

From Dev

How can I avoid interactive text UI stage during package installation?

From Dev

Netlogo How can I avoid the "dividing by 0" error during the dividing process compiling with Behavior space?

From Dev

In django how do I add another instance to an inline model before saving it?

From Dev

How to avoid property recursion

From Dev

How to avoid StackOverFlow in recursion?

From Dev

How to avoid property recursion

From Dev

Django : after saving model how not allow model to be updated anymore?

From Dev

django saving model with formset

From Dev

Django Model Form not saving

From Dev

How do I avoid unwanted recursion in my custom LinkedList implementation?

From Dev

How can I avoid this NullPointerException?

From Dev

How can i fill django model from external API?

From Dev

How can I set a DateField format in django from the model?

From Dev

How can I update object by model`s method, django

From Dev

How I can validate a Django form with a model attribute?

Related Related

  1. 1

    How can I avoid ProgrammingError: can't adapt type 'DateTimeRangeField' when saving a Django model instance to a remote database?

  2. 2

    How do I avoid saving duplicate data? [Django]

  3. 3

    How can I access a field during migration which is not in django model but available in databse

  4. 4

    How can I avoid events firing during InitializeComponent?

  5. 5

    Django conditional queries: How can I avoid this?

  6. 6

    How to avoid instant saving of the model with ajax validation?

  7. 7

    Can I avoid template recursion here?

  8. 8

    Saving path during tree recursion

  9. 9

    Recursion: how to avoid Python set changed set during iteration RuntimeError

  10. 10

    How can I avoid Slick's model declaration verbosity and repetitiveness

  11. 11

    Django - how can i call a model as a string?

  12. 12

    How can I model this behavior in Django?

  13. 13

    How can i stop this recursion?

  14. 14

    How can I avoid deadlocking when registering/deregistering observers during notification?

  15. 15

    How can I avoid interactive text UI stage during package installation?

  16. 16

    Netlogo How can I avoid the "dividing by 0" error during the dividing process compiling with Behavior space?

  17. 17

    In django how do I add another instance to an inline model before saving it?

  18. 18

    How to avoid property recursion

  19. 19

    How to avoid StackOverFlow in recursion?

  20. 20

    How to avoid property recursion

  21. 21

    Django : after saving model how not allow model to be updated anymore?

  22. 22

    django saving model with formset

  23. 23

    Django Model Form not saving

  24. 24

    How do I avoid unwanted recursion in my custom LinkedList implementation?

  25. 25

    How can I avoid this NullPointerException?

  26. 26

    How can i fill django model from external API?

  27. 27

    How can I set a DateField format in django from the model?

  28. 28

    How can I update object by model`s method, django

  29. 29

    How I can validate a Django form with a model attribute?

HotTag

Archive