old_contents: from django.db import models
  
  
  class StockReport(models.Model):
      form_id = models.CharField(max_length=100, db_index=True)
      date = models.DateTimeField(db_index=True)
      type = models.CharField(max_length=20)  # currently "balance" or "transfer"
  
      # todo: there are properties like these that could be really useful for queries
      # and reports - should decide which ones we want to add if any.
      # they are all redundant via form_id linkage
  
      # server_date = models.DateTimeField(default=datetime.utcnow, db_index=True)
      # user_id = models.CharField(max_length=100, db_index=True)
  
      def __repr__(self):
          return '{type} on {date} ({form})'.format(type=self.type, date=self.date, form=self.form_id)
  
  class StockTransaction(models.Model):
      report = models.ForeignKey(StockReport)
  
      stock_id = models.CharField(max_length=100, db_index=True)
  
      # for now, a supply point or requisition case
      case_id = models.CharField(max_length=100, db_index=True)
      product_id = models.CharField(max_length=100, db_index=True)
      # todo we should be more explicit about what belongs in this field
      type = models.CharField(max_length=20)  # i.e. 'loss' or 'receipt'
  
      # often one of these two will be derived based on the other one
      quantity = models.DecimalField(null=True, max_digits=20, decimal_places=5)
      stock_on_hand = models.DecimalField(max_digits=20, decimal_places=5)
  
      def __unicode__(self):
          return '{type} of {quantity} to {soh} (case: {case}, product: {product})'.format(
              type=self.type, quantity=self.quantity, soh=self.stock_on_hand,
              case=self.case_id, product=self.product_id,
          )
  
      def get_previous_transaction(self):
          siblings = StockTransaction._peer_qs(self.case_id, self.stock_id, self.product_id).exclude(pk=self.pk)
          if siblings.count():
              return siblings[0]
  
      @classmethod
      def latest(cls, case_id, stock_id, product_id):
          relevant = cls._peer_qs(case_id, stock_id, product_id)
          if relevant.count():
              return relevant[0]
          return None
  
      @classmethod
      def _peer_qs(self, case_id, stock_id, product_id):
          return StockTransaction.objects.filter(case_id=case_id, product_id=product_id).order_by('-report__date')
  
new_contents: from django.db import models
  
  
  class StockReport(models.Model):
      form_id = models.CharField(max_length=100, db_index=True)
      date = models.DateTimeField(db_index=True)
      type = models.CharField(max_length=20)  # currently "balance" or "transfer"
  
      # todo: there are properties like these that could be really useful for queries
      # and reports - should decide which ones we want to add if any.
      # they are all redundant via form_id linkage
  
      # server_date = models.DateTimeField(default=datetime.utcnow, db_index=True)
      # user_id = models.CharField(max_length=100, db_index=True)
  
      def __repr__(self):
          return '{type} on {date} ({form})'.format(type=self.type, date=self.date, form=self.form_id)
  
  class StockTransaction(models.Model):
      report = models.ForeignKey(StockReport)
  
      stock_id = models.CharField(max_length=100, db_index=True)
  
      # for now, a supply point or requisition case
      case_id = models.CharField(max_length=100, db_index=True)
      product_id = models.CharField(max_length=100, db_index=True)
      # todo we should be more explicit about what belongs in this field
      type = models.CharField(max_length=20)  # i.e. 'loss' or 'receipt'
  
      # often one of these two will be derived based on the other one
      quantity = models.DecimalField(null=True, max_digits=20, decimal_places=5)
      stock_on_hand = models.DecimalField(max_digits=20, decimal_places=5)
  
      def __unicode__(self):
          return '{type} of {quantity} to {soh} (case: {case}, product: {product}, stock id: {stock_id})'.format(
              type=self.type, quantity=self.quantity, soh=self.stock_on_hand,
              case=self.case_id, product=self.product_id, stock_id=self.stock_id,
          )
  
      def get_previous_transaction(self):
          siblings = StockTransaction._peer_qs(self.case_id, self.stock_id, self.product_id).exclude(pk=self.pk)
          if siblings.count():
              return siblings[0]
  
      @classmethod
      def latest(cls, case_id, stock_id, product_id):
          relevant = cls._peer_qs(case_id, stock_id, product_id)
          if relevant.count():
              return relevant[0]
          return None
  
      @classmethod
      def _peer_qs(self, case_id, stock_id, product_id):
          return StockTransaction.objects.filter(
              case_id=case_id, product_id=product_id, stock_id=stock_id).order_by('-report__date')
  
subject: fix query bug, update string representation of stocks to includ stock_id