Aggregation across foreign keys / complex aggregation and annotation
Following the general rule that no good deed goes unpunished, I have a question about complex aggregation / annotation or annotation across foreign keys. I have the following simplified model: class Unit(models.Model): unit = models.TextField() class Process(models.Model): process = models.TextField() unit = models.ForeignKey(Unit) class Exchange(models.Model): input = models.ForeignKey(Process) output = models.ForeignKey(Process) amount = models.FloatField() Is there a way for me to get a queryset for Process outputs which have a sum of inputs greater than one kilogram? You can do this (painfully) now by looping: foo = [] for p in Process.objects.all(): if sum(Exchange.objects.filter(ouput=p, input__unit__unit="kilogram").values_list('amount', flat=True)) > 1: foo.append(p) Sorry if this is a basic question, but I have been trying to figure it out for a while, and without success. -Chris --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Does using the IPython shell add "LIMIT" to Django ORM discussion (using SQLite)
Hello all- I recently filed a bug about incorrect SQL generation, and Malcom Tredinnick said that the example SQL I provided couldn't be correct, because there was an extra LIMIT clause that shouldn't be there. After poking around for a bit, I realized that everytime I was executing my query in the IPython shell, it was appending a LIMIT 21 to the SQL, but this limit clause wasn't being appended if the query was part of a regular python process. Both queries are run against SQLite 3. I uploaded a small test case here that demonstrates this behaviour: http://www.bitbucket.org/cmutel/extra_limit_21/ It is possible that I have poor python-fu, or is this known behaviour? Is it possible that Django is truncating the result list because IPython won't show all results anyway? I have tried searching the Django codebase, and the mailing list, but to no avail. -Chris --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
A twist on cross importing models
Hello all- I have tried a couple of tips found on the list to solve my problem with circular model imports, but with no lucks. Here is my basic schema: #foo/models.py from bar.models import Formula class Variable(models.Model): amount = models.FloatField(null=True) is_formula = models.BooleanField(default=False) formula = models.OneToOneField(Formula, null=True, related_name="variable_formula") def get_amount(self): if self.is_formula: return self.formula.evaluate() else: return self.amount #bar/models.py from foo.models import Variable class Formula(models.Model): formula = models.TextField() # Where formula is a text string like 'var:1337*var:42', and the numbers are id numbers. def calculate(self): # re stuff for each_found_variable in groups(): # Simplified to show process - this obviously doesn't work. raw_string.append(Variable.objects.get(id=this_id).get_amount()) return eval(raw_string, stuff_to_make_eval_safe) In this case, the circular reference occurs only in a method of the class - I don't know if this makes a difference. How can I structure my import statements so that this is possible? I should note that there are more than one variable, so that combining everything into one models file is not really possible (and doesn't really address the chicken/egg problem either). I thought I saw a recent svn commit that addressed this problem, but can't find it now. Any help would be greatly appreciated? -- Chris Mutel Ökologisches Systemdesign - Ecological Systems Design Institut f.Umweltingenieurwissenschaften - Institute for Environmental Engineering ETH Zürich - HIF C 42 - Schafmattstr. 6 8093 Zürich Telefon: +41 44 633 71 45 - Fax: +41 44 633 10 61 --~--~-~--~~~---~--~~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~--~~~~--~~--~--~---