Hi Karen, Thanks for your long reply. Though this problem has been solved (Thanks to all for your replies, especially Sam), let me explain a little more.
The 2nd for loop is actually an embedded loop in the 1st for loop. >From the 1st loop I retrieve some values, and use these values (as "model instance") to retrieve other values in the 2nd for loop. This logic is clear. And with Sam's help, I have got all those values that I wanted. I should say that the bad format in my original post should be blamed for this confusion. I noticed that you put the 2nd for loop outside the first for loop. This is not what I wanted. Thanks again. On Jul 13, 4:52 pm, Karen Tracey <kmtra...@gmail.com> wrote: > On Mon, Jul 13, 2009 at 5:36 PM, David <ww...@yahoo.com> wrote: > > > Thanks Amit. Here is the problem that I meet. > > > alerts = Alert.objects.filter((Q(dataset=dataset1) > > > for eachalert in alerts: > > e_metric1 = eachalert.criteria1_metric1 > > > Django complains that there is no such item "criteria1_metric1" in > > Alert class. This is correct as Alert class does not have such an > > item. The "criteria1_metric1" is a variable here that has a value. And > > this value is an item in the Alert class. > > What do you mean "this value is an item in the Alert class"? Items in the > Alert class are model instances which presumably have a set of different > field values. So I can't figure out what you by this sentence. > > > How to let Django know that the "criteria1_metric1" is a variable > > instead of a class item (table column)? > > Thanks again. > > The code you originally posted, reformatted to not wrap somewhat randomly > and removing the long strings of dots and line number is: > > alertcriteria1 = Alert_Criteria1.objects.all() > for eachcriterion in alertcriteria1: > dataset1 = eachcriterion.dataset > criteria1_metric1 = eachcriterion.criteria1_metric1 > alerts = Alert.objects.filter(Q(dataset = dataset1)) > for eachalert in alerts: > e_metric1 = eachalert.criteria1_metric1 > > First you loop through all of the Alert_Criteria1 objects. In that loop you > set a couple of variables, dataset1 and criteria1_metric1. As written, > these are local variables in your function. The loop seems pretty pointless > as at each iteration of the loop you simply overwrite the local variable > values from the last iteration. So on exit from the loop these two > variables will have values from the last item in > Alert_Criteria1.objects.all(). The values from all of the other > Alert_Criteria1 objects have not been saved anywhere. > > Then you loop through a different set of objects, Alert. And you try to set > a local variable, e_metric1, to be the value of the 'criteria1_metric1' > attribute of the current loop iteration's Alert model instance. But you say > that you know 'criteria1_metric!1' is not an attribute of an Alert, rather > you want what you set back up in the first loop. If that was what you > really wanted, your line of code would be: > > e_metric1 = criteria1_metric1 > > But I don't think that can be what you really want because it would be > pretty pointless to set the value of one local variable to be equal to while > looping over a set of objects. That line of code would have nothing to do > with the objects you are iterating over. > > If you could take a step back and explain what it is at a high level that > you are trying to do, someone may be able to help. As it is I suspect your > code is nowhere near doing what you are looking for, and solving the exact > attribute error or whatever it is you are seeing now is not really going to > get you any closer to whatever it is you are trying to achieve. > > Karen --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---