Home > Software design >  django-models set record value depends on the existig ones
django-models set record value depends on the existig ones

Time:08-06

yo, I have simple Question but it's answer sames to be not ,at all, maybe even not exist !

anyway, I just want to set a django-model field value based on existing ones, LIKE:

class Person(models.Model):
    #here is the struggle
    people = len(Person.objects.all())
     
    name = medels.Charfield()
    #pass it as argument
    peopleBeforeYou = models.CharField(default= people-1)

the idea is to set the peopleBeforeYou field default value to that same model QuerySet len()

any idea ????!!!!

CodePudding user response:

You could move the function that counts the number of existing rows to a separate function outside of the class

def count_people():
    return Person.objects.count()


class Person(models.Model):
    peopleBeforeYou = models.IntegerField(default=count_people)
  • Related