XiNiGaMi

Django

Jan 13th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.85 KB | None | 0 0
  1. #  The Models
  2.  
  3. class Product(models.Model):
  4.     name = models.CharField(null=True, max_length=30, )
  5.     price = models.IntegerField(default=0)
  6.     description = models.CharField(null=True, max_length=100, )
  7.  
  8. class Order(models.Model):
  9.     date = models.DateTimeField(auto_now_add=True, null=True)
  10.     table = models.IntegerField(null=False, max_length=2, )
  11.     waiter = models.ForeignKey(User, null=True,default=None)
  12.     products = models.ManyToManyField(Product,through='OrderProduct')
  13.     paid = models.BooleanField(default=False)
  14.     total = models.IntegerField(default=0,editable=False,null=True)
  15.  
  16.     @cached_property
  17.     def total(self):
  18.         x = 0
  19.         for product in self.products.all():
  20.             relationship_queryset = OrderProduct.objects.filter(order=self, product=product)
  21.             for p in relationship_queryset.all():
  22.                  x +=  p.total_products
  23.         return x
  24.  
  25. class OrderProduct(models.Model):
  26.     order = models.ForeignKey(Order)
  27.     product = models.ForeignKey(Product)
  28.     products_quantity = models.IntegerField(max_length=2)
  29.     products_total = models.IntegerField(default=0,editable=False)
  30.  
  31.     def save(self, *args, **kwargs):
  32.         self.products_total = (self.product.price * self.products_quantity)
  33.         super(OrderProduct, self).save(*args, **kwargs)
  34.  
  35.  
  36.  
  37. # the view
  38. def sales(request):
  39.   today = datetime.utcnow()
  40.   today_sales = m.Order.objects.filter(date=today).all()
  41.   return render_to_response('pages/sales.html',{'request':request,'sales':today_sales})
  42.  
  43. # the template
  44. {% for sale in sales %}
  45.     <ul>{{ sale.date }}|{{ sale.paid }}|{{ sale.total }}</ul>
  46.     !! Here i wanna put the products from the current sale !!
  47.     {% for product in sales.products %}
  48.         {{ product.name }}|{{ product.price }}|{{ product.products_quantity }}|{{ product.products_total }}
  49.     {% endfor %}
  50. {% endfor %}
Advertisement
Add Comment
Please, Sign In to add comment