Skip to content

Instantly share code, notes, and snippets.

@felipe-prenholato
Created November 6, 2012 16:33
Show Gist options
  • Save felipe-prenholato/4025844 to your computer and use it in GitHub Desktop.
Save felipe-prenholato/4025844 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
from __future__ import (absolute_import, division, print_function,
unicode_literals)
from django.db import models
class Product(models.Model):
""" Store products """
name = models.CharField(max_length=50)
class Locality(models.Model):
""" Store localitys """
name = models.CharField(max_length=50)
class Supply(models.Model):
""" A product and a locality make a supply with N items """
product = models.ForeignKey(Product, related_name='supply')
locality = models.ForeignKey(Locality, related_name='supply')
item_amount = models.PositiveInterField()
class Price(models.Model):
"""
Price is used to calculate stock price at end of month, it changes when
new items is added. We always have 1 for each month.
"""
supply = models.ForeignKey(Supply, related_name='prices')
price = models.DecimalField(default=0, max_digits=8, decimal_places=2)
year = models.PositiveInterField()
month = models.PositiveInterField(min_value=1, max_value=12)
class Ticket(models.Model):
"""
Ticket is a simple class to centralize tickets for 'office supplies',
'post office', and other systems.
A new ticket is created every time someone want something from some system.
"""
date_created = models.DateTimeField(auto_create_now=True, editable=False)
date_finished = models.DateTimeField(editable=False, null=True, default=None)
user = models.ForeignKey(User, related_name='tickets')
class TicketItem(models.Model):
"""
Since Tickets are used by many other systems (ex, post office), this model
is the link between Tickets and Supplies.
"""
ticket = models.ForeignKey(Ticket)
supply = models.ForeignKey(Supply)
amount_out = models.PositiveInterField()
"""
I need to get every ticket item requested with it price, so fields that I need,
starting from TicketItem, is:
ticket id, ticket date created, ticket date finished, supply price for same date
of ticket date finished, supply localityuntitled, supply product
With following query I get everything, except price:
TicketItem.objects.values_list('ticket__id', 'ticket__date_created',
'ticket__date_finished', 'supply__product__name', 'supply__locality__name')
Unfortanately, using F dont help me:
TicketItem.objects.filter(supply__prices__year=F('ticket__date_created__year')\
.values_list('ticket__id', 'ticket__date_created', 'ticket__date_finished',
'supply__product__name', 'supply__locality__name')
How I can do it with django offers?
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment