class Bill(models.Model): def __number(): """ Automatic bill number generator. bill_emmited: returns a list with all the bill objects with bill_type=False latest_object: from bill_emmited get the latest one and convert it to string to manipulate first_number: returns the first number of the latest_object field and adds 1 If there's no bill objects, return first_number=1. The overall function returns either an incremented first_number slash year or one slash year. """ current_year = date.today().strftime('%y') try: bill_emmited = Bill.objects.filter(bill_type__exact=False) latest_object = bill_emmited.latest('number').__str__() first_number = int(latest_object.split("/")[0]) + 1 except Bill.DoesNotExist: first_number = 1 return '%s/%s' % (first_number, current_year) number = models.CharField(_('Bill number'), max_length=10, unique=True, \ default=__number) bill_type = models.BooleanField()