Karen
Thanks for your reply. The reason I was trying to filter the id with
count was that I had read somewhere that you have to pass sum at least
two arguments (but may be I mis-understood what I was reading!)
I have changed my code to:
def get_fringe_value(self):
ft =
Fringe.objects.select_related().filter(id__in=self.fringe.all()).values()
qft=ft(sum('percentage'))
return u'%d'%qft
fringe_value = get_fringe_value
def save(self):
self.native_total = self.quantity*self.multiplier*self.value
self.currency_total =
self.quantity*self.multiplier*self.value*self.currency_code.rate1
self.variance = self.currency_total-self.prev_total
self.fringe_total = self.fringe_value()
super(Detail, self).save()
Now I get the error:
File "/usr/lib/python2.5/site-packages/django/contrib/admin/options.py"
in save_model
597. obj.save()
File "/home/trevor/1stdjangoproject/mysite/../mysite/bt4/models.py" in save
147. self.fringe_total = self.fringe_value()
File "/home/trevor/1stdjangoproject/mysite/../mysite/bt4/models.py" in
get_fringe_value
137. qft=ft(sum('percentage'))
Exception Type: TypeError at /admin/bt4/detail/1/
Exception Value: unsupported operand type(s) for +: 'int' and 'str'
I presume this is because I'm trying to convert a string to decimal when
it doesn't have an integer type content(again I could be completely wrong)
How can I extract the Decimal value that is associated with the field
'percentage'?
Here's the line from the admin debug screen which shows the values that
ft is picking up - I would just like to sum zero, 1 or more of the
'percentage' values when they are tagged(associated) to the current record.
ft
[{'ceiling': None, 'code': u'NI', 'description': u'UK National
Insurance', 'floor': None, 'flat_rate': None, 'percentage':
Decimal("0.128"), 'total': None, 'id': 1}, {'ceiling': None, 'code':
u'HP', 'description': u'Holiday Pay', 'floor': None, 'flat_rate': None,
'percentage': Decimal("0.1207"), 'total': None, 'id': 2}]
I apologise for these naive questions but I have to resolve this so I
can understand how to build my app and continue with other functionality
which will have a similar purpose.
Many thanks - Trevor
Karen Tracey wrote:
On Sat, Jan 22, 2011 at 9:43 AM, TAS <h...@balancingact.me.uk
<mailto:h...@balancingact.me.uk>> 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 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.
--
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.