tags:

views:

45

answers:

1

This Is my models.py

class Customer(models.Model):
    def __unicode__(self):
        return self.name
    name = models.CharField(max_length=200)
    type = models.ForeignKey(Customer_Type)
    active = models.BooleanField(default=True)

class Sale(models.Model):
    def __unicode__(self):
        return "Sale %s (%i)" % (self.type, self.id)
    customer = models.ForeignKey(Customer)
    total = models.DecimalField(max_digits=15, decimal_places=3)

class Unitary_Sale(models.Model):
    book = models.ForeignKey(Book)
    quantity = models.IntegerField()
    unit_price = models.DecimalField(max_digits=15, decimal_places=3)
    sale = models.ForeignKey(Sale)

Views.py

def get_filter_result(self, customer_type='' volume_sale=''):
        qdict = {}
        if customer_type != '':
            qdict['type__name'] = customer_type
            qdict['active']=True
        #WHAT I AM GOING TO DO NEXT
   ***  if volume_sale != '':
            pass # This point I am asking :)
        #IT RETURN CUSTOMERS BASE ON PARAMS.
        queryset = Customer.objects.filter(**qdict)

***The volume_sale is:

units=Unitary_Sale.objects.all()
>>> units=Unitary_Sale.objects.all()
>>> for unit in units:
...    print unit.sale.customer
...    print unit.book,unit.sale.total
...
Sok nara
Khmer Empire (H001) 38.4
Sok nara
killing field (H001) 16

San ta
khmer krom (H001) 20
San ta
Khmer Empire (H001) 20
>>>
{<Customer: Sok nara>: Decimal("56.4"), <Customer: san ta>: Decimal("40")}

Decimal("56.4") , Decimal("40") this is the volume_sale

I could not find the ways to make the filter from difference object as in my case. It will be great if everyone here help in this stuck? Thanks.

+2  A: 

Cool, this actually pretty easy to implement. I used the django annotation feature documented here and here:

from django.db.models import Sum
query = Customer.objects.all().annotate(volume_sale = Sum('Sale__total'))
query.filter(volume_sale < 12.0) #return all customers that have purchased less than 12.0
query[0].volume_sale #you can even get it on the Customer objects given back

Django will take care of the database joins for you. It will put this extra field into each instance of the model that you can filter, order_by and access in templates or views.

JudoWill
I used Django version 1.0.2 final, ImportError: cannot import name sum
python
@python ... did you capitalize the 'S' in Sum ? ... your copy-paste has a lower-case 's'
JudoWill
In fact I tried both.from django.db.models import Sumand from django.db.models import sum
python
from django.db.models import Avg, Max, Min, Countall of this I can not import
python
hmmm ... that I have no idea about ... maybe something weird in your path ... but then you'd have problems running the ORM completely ... might be another good stackoverflow question ;)
JudoWill
The aggregation features including Sum were only added in Django 1.1.
Daniel Roseman
okay,you are right @Daniel .after I changed to 1.1 it works. !
python
wow ... I don't know why I thought it was added in 1.0 ... great catch @Daniel
JudoWill
@JuboWill It works when I changed to django1.1 .Are there other ways for my version 1.0.2 final.? I dont wish to change django version.THX
python
@python Somone smarter than me could figure out how to use the `queryset.select()` and put the raw SQL in there ... but I use the ORM because my SQL-fu is pretty crummy. You could probably post another SO-question, link to this one and specifically mention that you're using django 1.0 and need an answer without using aggregation.
JudoWill