I am struggling to grasp  the "full picture" when it comes to view queries
and associated templates which deal with sets of "nested"  parent/child
relationships, and am hoping someone here can enlighten me.  All the
examples I have seen seem to deal with simple views showing one, or maybe
two models at a time with very little processing (e.g. calculations).

A "typical" (but very very simplified) sales model for a business could look
something like this:

class Area(models.Model):
    ZONE_CHOICES = (
        (u'U', u'USA'),
        (u'E', u'Europe'),
        (u'A', u'Asia'),
    )
    name = models.CharField(max_length=50)
    zone = models.CharField(maxlength =1, choices=ZONE_CHOICES)

class Customer(models.Model):
    name = models.CharField(maxlength = 50)
    phone = models.CharField(maxlength = 50)
    area = models.ForeignKey(Area)

class Order(models.Model):
    date = models.DateField()
    customer = models.ForeignKey(Customer)
    shipped = models.BooleanField() #has order been shipped?

class Widget(models.Model):
    SIZE_CHOICES = (
        (u'1', u'Small'),
        (u'2', u'Medium'),
        (u'3', u'Large'),
    )
    name = models.CharField(maxlength = 50)
    size =  models.IntegerField(choices=SIZE_CHOICES)
    price = models.FloatField()

class OrderDetails(models.Model):
    widget = models.ForeignKey(Widget)
    order = models.ForeignKey(Order)
    number = models.IntegerField() #number of widgets purchased

A typical report (Django view+template) that someone might want to see, is
to show all orders for the current year, with order details, along with
totals (counts and cost) at customer and order levels,  for a given set of
criteria e.g. all customers in Europe, who have ordered widgets of size 2,
with orders that have a total value of $300 or more, in 2010.

The report would need to be laid out in a hierarchical fashion, showing:
alphabetically sorted customer name / phone and their total no. of orders,
followed below by each order in date sequence - date, id and shipping status
- followed below by the order details for that order (line for each widget,
with name, size, number purchased and total cost), followed at the end by
the total order cost.  At the very end of the report, after all customers
are displayed, the total cost of all orders, plus the average order cost.

I would really like to see how (a) the view code would be constructed and
(b) what the corresponding template specification would be.  I can see, as a
start, that the view might look something like:

def view_order_details (request, zone, year, size, order_value):
    customers = Customer.objects.filter(area__zone=zone)

But here is where I get stuck!  How do I "nest" the  orders for each
customer, and also the order details (and the associated calculations)?
What type of data structure gets created as a result?  When does it get
created? What does the template code, in turn, look like to be able to
process this structure (or structures)?

Note 1: Please note this is NOTHING at all like the models I am using - in
case anyone thinks I am asking them to do my work! - its so that I can
understand how such a nested system would be constructed - and how the
template, in turn, would process it.

Note 2:  Yes, I realize these set of models could be improved with
many-to-many or one-to-many relationship fields added.  But, if at all
possible, please just work with it "as is".


Thanks - this will really help improve my understanding of a crucial
conceptual area.
Derek

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to