title: Django, accessing PostgreSQL sequence
body: <p>In a Django application I need to create an order number which looks like: yyyymmddnnnn in which yyyy=year, mm=month, dd=day and nnnn is a number between 1 and 9999.</p>
  
  <p>I thought I could use a PostgreSQL sequence since the generated numbers are atomic, so I can be sure when the process gets a number that number is unique.</p>
  
  <p>So I created a PostgreSQL sequence:</p>
  
  <pre><code>CREATE SEQUENCE order_number_seq
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9999
  START 1
  CACHE 1
  CYCLE;
  </code></pre>
  
  <p>This sequence can be accessed as a tables having one row. So in the file checkout.py I created a Django model to access this sequence.</p>
  
  <pre><code>class OrderNumberSeq(models.Model):
      """
      This class maps to OrderNumberSeq which is a PostgreSQL sequence.
      This sequence runs from 1 to 9999 after which it restarts (cycles) at 1.
      A sequence is basically a special single row table.
      """
      sequence_name = models.CharField(max_length=128, primary_key=True)
      last_value = models.IntegerField()
      increment_by = models.IntegerField()
      max_value = models.IntegerField()
      min_value = models.IntegerField()
      cache_value = models.IntegerField()
      log_cnt = models.IntegerField()
      is_cycled = models.BooleanField()
      is_called = models.BooleanField()
  
      class Meta:
          db_table = u'order_number_seq'
  </code></pre>
  
  <p>I set the sequence_name as primary key as Django insists on having a primary key in a table.</p>
  
  <p>The I created a file get_order_number.py with the contents:</p>
  
  <pre><code>def get_new_order_number():
      order_number = OrderNumberSeq.objects.raw("select sequence_name, nextval('order_number_seq') from order_number_seq")[0]
  
      today = datetime.date.today()
      year = u'%4s' % today.year
      month = u'%02i' % today.month
      day = u'%02i' % today.day
  
      new_number = u'%04i' % order_number.nextval
      return year+month+day+new_number
  </code></pre>
  
  <p>now when I call 'get_new_order_number()' from the django interactive shell it behaves as expected.</p>
  
  <pre><code>&gt;&gt;&gt; checkout.order_number.get_new_order_number()
  u'201007310047'
  &gt;&gt;&gt; checkout.order_number.get_new_order_number()
  u'201007310048'
  &gt;&gt;&gt; checkout.order_number.get_new_order_number()
  u'201007310049'
  </code></pre>
  
  <p>You see the numbers nicely incrementing by one every time the function is called. You can start multiple interactive django sessions and the numbers increment nicely with no identical numbers appearing in the different sessions.</p>
  
  <p>Now I try to use call  this function from a view as follows:</p>
  
  <pre><code>import get_order_number
  
  order_number = get_order_number.get_new_order_number()
  </code></pre>
  
  <p>and it gives me a number. However next time I access the view, it increments the number by 2. I have no idea where the problem is.</p>