On Tue, Feb 10, 2009 at 8:43 PM, Alessandro <alessandro.ron...@gmail.com> wrote: > > > 2009/2/10 Russell Keith-Magee <freakboy3...@gmail.com> >> >> >> However, if you're using Django trunk, what you want is: >> >> >>> from django.db.models import Max >> >>> qs = >> >>> Module_scheme.objects.filter(plant=3).annotate(max_energy_tot=Max('module__energy_tot')) >> >>> for mod in qs: >> ... print mod.id, mod.max_energy_tot >> >> That is - retrieve a list of Module_scheme objects associated with >> plant 3, and annotate each one of which is annotated with the maximum >> of the energy_tot of the modules related to the Module_scheme. The >> maximum is provided as an extra attribute on each of the Module_scheme >> instance. > > I cannot do this because I have a ForeignKey from Module to Module_Scheme, > and your first line returns > > FieldError: Cannot resolve keyword 'module' into field. Choices are: id, > jbox, module_number, modules, plant, rack_number
Apologies - I misread the name of your class. The query should read: Module_scheme.objects.filter(plant=3).annotate(max_energy_tot=Max('modules__energy_tot')) i.e., plural modules, not singular module, in the Max lookup. It's the reverse lookup name for the module relationship on Module_scheme. For future reference, the message on the FieldError tells you the right name - modules. Yours, Russ Magee %-) --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---