Hi. I am a newbie  :)

This is probably not a very Django-like design at this point, but the
goal is to move towards it from a legacy-type design DB.

My models are currently set up something like the following (since
there is quite a bit of trial-and-error database design and data
import work to see how to best design/redesign still at this stage,
most of the fields are allowed to be blank and NULL, hopefully this
will be resolved and DB design will be finalized):

class Product(models.Model):
    ndc = models.CharField(max_length=50, blank=True, null=True)
    product_name = models.CharField(max_length=50, blank=True, null=True)
    quantity = models.IntegerField(blank=True, null=True)
    product_description = models.CharField(max_length=50, blank=True, null=True)

class Pricing(models.Model):
    ndc = models.ForeignKey(Product)
    wac = models.DecimalField(max_digits=17, decimal_places=8,
blank=True, null=True)
    wac_date = models.DateField(blank = True, null = True)
    wacdate_end = models.DateField(blank = True, null = True)

class Contract(models.Model):
    products = models.ManyToManyField(Product, through='ContractProduct')
...

class ContractProduct(models.Model):
    contract = models.ForeignKey(Contract)
    ndc = models.ForeignKey(Product)
    rebate_pct = models.DecimalField(max_digits=17, decimal_places=8)
    wac = models.ForeignKey(Pricing)

class Claims(models.Model):
    contract = models.ForeignKey(Contract)
    ndc = models.ForeignKey(Product)
    date = models.DateField(blank=True, null=True)
    claim_number = models.CharField(max_length=50, blank=True, null=True)
    quantity = models.IntegerField(blank=True, null=True)
    prices = models.ManyToManyField(Pricing)                 # likely not needed
...

One of my views returns many objects from the Claims table and
iterates over them to calculate some values based on all the objects
in the query. In order to optimize things a bit and not do a bazillion
queries inside the for loop, I build small dictionaries and iterate
over those instead, something like the following:

@login_required
def agreement(request, contract_id, quarter, paginate_by=30, **kwargs):
    c = Contract.objects.get(id = contract_id)

    p_per_c=c.products.all()

    c_dict = dict((product.id, product.contractproduct_set.get(
        contract=contract_id,ndc=product.id).rebate_pct)
        for product in p_per_c)

    p_dict = dict((product.id, product.contractproduct_set.get(
        contract=contract_id, ndc=product.id).wac.wac)
        for product in p_per_c)


    yearnow = datetime.date.today().year

    quarters = [ [datetime.date(yearnow, 1, 1), datetime.date(yearnow, 3, 31)],
                 [datetime.date(yearnow, 4, 1), datetime.date(yearnow, 6, 30)],
                 [datetime.date(yearnow, 7, 1), datetime.date(yearnow, 9, 30)],
                 [datetime.date(yearnow, 10, 1), datetime.date(yearnow, 12, 31)]
                                                                              ]
    start_date = quarters[int(quarter)-1][0]
    end_date =   quarters[int(quarter)-1][1]

    cset = Claims.objects.filter(contract = contract_id,
            rebate_period_start = start_date, rebate_period_end__lte = end_date)

    t_rebate = 0; t_wac_sales = 0

    for row in cset:
        rebate_pct = c_dict[row.ndc_id]
        rebate = row.quantity * p_dict[row.ndc_id] * rebate_pct
        wac_sales = row.quantity * p_dict[row.ndc_id]
        t_wac_sales += wac_sales
        t_rebate += rebate


Is there a way to rewrite the dictionary code above to make it more
readable, and/or rewrite everything in a more efficient manner in
general? It still takes a bit of time to run (but much faster than
doing queries inside the loop).

If I am doing anything glaringly wrong, or if you have any other
suggestions please let me know. I really appreciate your help.

Thanks.

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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