Number generator to autofill a field

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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()

More like this

  1. models.py with django_dag models for parts hierarchy by j_syk 1 year, 9 months ago
  2. Jquery ajax csrf framework for Django by chriszweber 1 year, 4 months ago
  3. Date/time util template filters by marinho 5 years, 6 months ago
  4. locale based on domain by zeeg 5 years, 11 months ago
  5. Sorl Thumbnail + Amazon S3 by skoczen 3 years, 11 months ago

Comments

(Forgotten your password?)