1

I have a custom .save method on my model. The model has a start_date and a last_edited value. If the difference between these values is more than 14 days, it should copy/clone itself on save (instead of saving).

You probably already see the problem: infinite recursion. If the clone saves itself, the values will still differ 14 days and the whole copy process will start anew.

Therefore I want to to pass these copies a 'copy' argument in their .save parameter, in order to prevent them from triggering the copy process themselves.

To this end I've written the following code:

def save(self, *args, **kwargs):

    #check if a submission is older than the 'create a new one' threshold
    #Create a new one if that is the case

    delta = self.last_edited - self.start_date

    print(args)
    print(kwargs)

    if 'copy' in kwargs or 'copy' not in args:
        print('cloning!')

        if delta.days >= 14:

            clone = self
            clone.pk = None
            clone.save('copy')

    super(AssessmentSubmission, self).save(*args, **kwargs)

However, for some reason clone.save(copy) does not pass the 'copy' variable to the .save method. I even added some print statements to print all args and kwarg arguments, but both return empty lists/ dicts.

Does anyone know what I am doing wrong?

1 Answer 1

1

Why don't you set a flag field in the model? Cleaner than *kwargs and **args. Something like:

class AssessmentSubmission(models.Model):
    '''
    Your other fields here
    '''
    flag_field = models.IntegerField(default=0,blank=True,null=True)

    def save(self):

    #check if a submission is older than the 'create a new one' threshold
    #Create a new one if that is the case

    delta = self.last_edited - self.start_date

    print(args)
    print(kwargs)

    if not self.flag_field:
        print('cloning!')

        if delta.days >= 14:

            clone = self
            clone.pk = None
            clone.flag_field = 1
            clone.save('copy')

    super(AssessmentSubmission, self).save()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.