python - How to change record of object in Django's model instantly? -
i have class , objects, want when save new record, django checks if "is_it_true" false or true, if true change default value of "number" new value corresponded count of objects have "is_it_question" plus one; , if false, default of "number" remain defaut(100). should do?
class typefourchoice(models.model): question_choice = models.foreignkey(typefour) is_it_question = models.booleanfield(default=false) number = models.positiveintegerfield(blank=false, default=100) word_or_words = models.charfield(default='', blank=false, max_length=20) timestamp = models.datetimefield(auto_now_add=true) def __str__(self): return "{}".format(self.question_choice) @classmethod def save(cls, *args, **kwargs): no = cls.objects.exclude(number=100).count() if no none: pass else: clss = cls.objects.get(???????) if clss.is_it_question: no += 1 clss.number = no clss.save(*args, **kwargs) else: clss.number = 100 clss.save(*args, **kwargs)
you should remove @classmethod
decorator save()
method. , simplify method this:
def save(self, *args, **kwargs): # new objects have no `pk` if self.pk none , self.is_it_question: self.number = typefourchoice.objects.filter(is_it_question=true) \ .count() + 1 super(typefourchoice, self).save(*args, **kwargs)