Monday, July 26, 2010

Custom method to query external db

In one of my models, 3 values refer to unique ID's in an external db I
want to query for a sum for each ID.
What's the best way to query this external MySQL db?

Example
======
Model:
---------
class Test(models.Model):
name = models.CharField(max_length=10)
id1 = models.PositiveIntegerField(max_length=4)
id2 = models.PositiveIntegerField(max_length=4)
id3 = models.PositiveIntegerField(max_length=4)

DB Query:
---------------
select sum(value) from table_test where id1=%i % (id)

Problem(s):
----------------
How do I implement this the optimal way? I obviously don't want x
model instances to run this query 3 times.
(3 times (for id1, id2, id3) is no problem)


--
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+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment