On Sat, Jan 22, 2011 at 9:43 AM, TAS <[email protected]> wrote:
> def get_fringe_value(self):
> ft =
> Fringe.objects.select_related().filter(id__in=self.fringe.all()).values()
> if ft.count()==0:
> return u'%d'(0)
> if ft.count()==1:
> qft=ft.get('percentage')
> return u'%d'(qft)
>
You only set qft to something if ft.count() is 1. Yet the next line of code
(the return, which uses the value of qft) is not indented to the same level
as the assignment of qft, so it is not part of the ft.count() ==
1conditional: it is going to be executed regardless of the ft.count() value.
In the case where ft.count() is anything other than 1, that line of code is
attempting to use the value of a variable that has not yet been set to
anything. It can't complete successfully because the compiler doesn't know
what to return for the value of qft since it has not been set to anything.
That is what the message "local variable 'qft' referenced before assignment'
means.
> if ft.count()>=2:
> aqft=ft.aggregate(sum('percentage'))
> return u'%d'(aqft)
>
Note you'll never get to this code since you unconditionally return in the
line above it. It also looks like it has the same problem of the code above,
in that it sets the variable in a conditional block but then the return
statement that uses the variable value isn't in that block. Perhaps you'll
never have the same issue, since the cases where you would not take the
conditional branch have already been dealt with above, but if that's the
case -- if your code by this point has already ensured that ft.count() must
be >= 2 then that conditional ought not be there at all.
Once you get past the error referencing a variable that has not been set,
you are going to hit another problem with all of those return statements:
the compiler, when it tries to run any of them, is going to report
TypeError: 'unicode' object is not callable. If you are trying to return a
unicode string containing the value you need to put the interpolation
operator % in between the string and values you want to embed in it: u'%d' %
qft.
Beyond that, this whole routine looks not quite right. I don't have time to
figure out what it is exactly you are trying to calculate, but whatever it
is your code should not need to have different legs for the case where there
are 0, 1, or more than 1 related things. If you need a sum of values from
the related things the code to get it should be the same regardless of how
many related instances there are.
Karen
--
http://tracey.org/kmt/
--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected].
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.