Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.
  • Root
  • » Django
  • » Can someone help with Many-to-many referencing and then calculations against another field on the linked model [RSS Feed]

#1 Jan. 20, 2011 00:43:18

Kenneth G.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Can someone help with Many-to-many referencing and then calculations against another field on the linked model


On Wed, 2011-01-19 at 22:34 +0000, The Stanley Household wrote:
> Happy to send Model if needed but at the moment I'm unable to run the
> server to provide admin error report, can C&P from terminal if that
> would help with the error.

please copy and paste your code - it is easier for us to help when we
see what you are doing
--
regards
KGhttp://lawgon.livejournal.comCoimbatore LUG roxhttp://ilugcbe.techstud.org/--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Jan. 20, 2011 08:30:37

Trevor S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Can someone help with Many-to-many referencing and then calculations against another field on the linked model


Kenneth Gonsalves wrote:On Wed, 2011-01-19 at 22:34 +0000, The Stanley Household wrote:Happy to send Model if needed but at the moment I'm unable to run the
server to provide admin error report, can C&P from terminal if thatwould help with the error.please copy and paste your code - it is easier for us to help when we
see what you are doingKennethHere is the code. I've added some notes to try and explain what I thinkI'm doingclass Fringe(models.Model):
code = models.CharField(max_length=3, unique = 'true')
description = models.CharField(max_length=50)percentage = models.DecimalField(max_digits=6, decimal_places=5,default=0)flat_rate = models.IntegerField(blank=True, null=True)
floor = models.IntegerField(blank=True, null=True)
ceiling = models.IntegerField(blank=True, null=True)
total = models.IntegerField(blank=True, null=True)

def __unicode__(self):
return self.code


class Detail(models.Model):
account = models.ForeignKey(Account, related_name= 'account_account')
description = models.TextField(max_length=200)
fringe = models.ManyToManyField(Fringe, blank=True, null=True)
location = models.ForeignKey(Location, blank=True, null=True)
set_code = models.ForeignKey(Set_Group, blank=True, null=True)
flag = models.ManyToManyField(Flag, blank=True, null=True)
quantity = models.IntegerField(blank=True, null=True)
units = models.CharField(max_length=10)
multiplier = models.IntegerField(default=1)
value = models.IntegerField(blank=True, null=True)
native_total = models.IntegerField(blank=True, null=True)
currency_code = models.ForeignKey(Currency)
currency_total = models.IntegerField(blank=True, null=True)
prev_total = models.IntegerField(default = 0)
variance = models.IntegerField(blank=True, null=True)
sort = models.IntegerField()cash_flow_code = models.ForeignKey(Cashflow, related_name='cashflow_code')cash_flow_start_week = models.IntegerField(blank=True, null=True)
cash_flow_weeks = models.IntegerField(blank=True, null=True)
notes = models.TextField(max_length=200, blank=True, null=True)fringe_total = models.DecimalField(max_digits=6,decimal_places=5,blank=True, null=True)def __unicode__(self):return self.descriptionclass Meta:ordering = @models.permalinkdef get_absolute_url(self):
return('bt4_edit_detail', (), { 'object_id':self.id })def _get_fringe_value(self):ft =Fringe.objects.select_related().filter(id=self).values()"""select recordby ID from the M2M connection fringes"""qft=ft#.filter(id=self).values()"""Now obsolete as added to lineabove will amend when I get it working""""""so as not to pass a single value to sum if only one record
passed from ft then just pass value of percentage"""
if qft.count()<=1:
aqft=qft.filter('percentage')"""if more than one record returned from related model do sum onpercentage"""else:
aqft=qft.annotate(sum('percentage'))"""return value as decimal as it is actually a percentage whichwillbe used to create a new value from currency_total on save"""
return u'%d' (aqft)

fringe_value = _get_fringe_value('aqft')

def save(self):
self.native_total = self.quantity*self.multiplier*self.valueself.currency_total =self.quantity*self.multiplier*self.value*self.currency_code.rate1self.variance = self.currency_total-self.prev_total
self.fringe_total = self.fringe_value
super(Detail, self).save()

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Jan. 20, 2011 09:31:49

Kenneth G.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Can someone help with Many-to-many referencing and then calculations against another field on the linked model


On Thu, 2011-01-20 at 08:37 +0000, Trevor Stanley wrote:
> Fringe.objects.select_related().filter(id=self)

should this not be (id=self.id)
--
regards
KGhttp://lawgon.livejournal.comCoimbatore LUG roxhttp://ilugcbe.techstud.org/--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Jan. 20, 2011 18:57:23

Trevor S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Can someone help with Many-to-many referencing and then calculations against another field on the linked model


KennethThat is what I originally though but if I write that this is the error Iget:in _get_fringe_value
ft = Fringe.objects.select_related().filter(id=self.id).values()
AttributeError: 'str' object has no attribute 'id'I'm in London and I do this in my spare time so have just arrived homefrom work. Many thanks for looking at this for me.Kenneth Gonsalves wrote:On Thu, 2011-01-20 at 08:37 +0000, Trevor Stanley wrote:Fringe.objects.select_related().filter(id=self)should this not be (id=self.id)--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 Jan. 20, 2011 23:08:28

R.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Can someone help with Many-to-many referencing and then calculations against another field on the linked model


On Jan 20, 2:04 pm, Trevor Stanley <h...@balancingact.me.uk> wrote:
> Kenneth
>
> That is what I originally though but if I write that this is the error I
> get:
>
> in _get_fringe_value
>     ft = Fringe.objects.select_related().filter(id=self.id).values()
> AttributeError: 'str' object has no attribute 'id'
>
> I'm in London and I do this in my spare time so have just arrived home
> from work.  Many thanks for looking at this for me.
>
>

This line isn't right:

fringe_value = _get_fringe_value('aqft')

Not sure what you're trying to do, if first arg
to that function is self, you should use it
as an instance method, e.g.

detail = Detail(...)
detail._get_fringe_value()

If you want to pass it a second arg, change
def line to accept a 2nd arg and call with:

detail._get_fringe_value('aqft')

-ak

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#6 Jan. 21, 2011 02:04:23

Kenneth G.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Can someone help with Many-to-many referencing and then calculations against another field on the linked model


On Thu, 2011-01-20 at 19:04 +0000, Trevor Stanley wrote:
> ft = Fringe.objects.select_related().filter(id=self.id).values()
> AttributeError: 'str' object has no attribute 'id'

maybe filter(id=self.fringe) or (id=self.fringe_id)
--
regards
KGhttp://lawgon.livejournal.comCoimbatore LUG roxhttp://ilugcbe.techstud.org/--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#7 Jan. 21, 2011 05:31:47

Kenneth G.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Can someone help with Many-to-many referencing and then calculations against another field on the linked model


On Fri, 2011-01-21 at 07:33 +0530, Kenneth Gonsalves wrote:
> On Thu, 2011-01-20 at 19:04 +0000, Trevor Stanley wrote:
> > ft = Fringe.objects.select_related().filter(id=self.id).values()
> > AttributeError: 'str' object has no attribute 'id'
>
> maybe filter(id=self.fringe) or (id=self.fringe_id)

no - that is wrong. filter(id__in=self.fringe.all())
--
regards
KGhttp://lawgon.livejournal.comCoimbatore LUG roxhttp://ilugcbe.techstud.org/--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#8 Jan. 21, 2011 19:28:52

Trevor S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Can someone help with Many-to-many referencing and then calculations against another field on the linked model


RainyThanks for the feedback and I'm sure you are right that the line isincorrect. However I need a bit of instruction. I have readextensively over the last year but I think I must be missing afundamental thing. This is the first programming I've ever done so youmay need to spell it out in the most simplest terms.My aim is to take the decimal value of the percentage field on theFringe table and use it to multiply against the currency_total value onthe Detail table of the record and then store that on the Detail table.There may be more than one fringe for a row and so the percentages needto be summed before they are sent to the save. I was hoping I hadwritten that with the _get_fringe_value routine -but maybe not!What I think I've not understood is how these processes are called andwhether I have put this coding in the right place. As I am trying tosave values at the same time I put the def _get_fringe_values under theDetail class. Should I be in fact creating a model.Manager to run thisprocess and then call that on save. On my readings I thought modelmanagers were used at table level rather than row level and I think whatI'm trying to do is effect a row not a table.I've been looking through Definitive guide to Django, Practical Djangoprojects and Pro Django but they are more concerned with getting therelationship of the models understood (which is no bad thing) ratherthan the actual syntax when writng code. I have worked through somePython tutorials online but still struggle to test my code via theshell! All this leads me to going around in ever decreasing circles - Ihope you can help me out of the fog.-TS


Rainy wrote:On Jan 20, 2:04 pm, Trevor Stanley <h...@balancingact.me.uk> wrote:Kenneth

That is what I originally though but if I write that this is the error I
get:

in _get_fringe_value
ft = Fringe.objects.select_related().filter(id=self.id).values()
AttributeError: 'str' object has no attribute 'id'

I'm in London and I do this in my spare time so have just arrived home
from work. Many thanks for looking at this for me.This line isn't right:

fringe_value = _get_fringe_value('aqft')

Not sure what you're trying to do, if first arg
to that function is self, you should use it
as an instance method, e.g.

detail = Detail(...)
detail._get_fringe_value()

If you want to pass it a second arg, change
def line to accept a 2nd arg and call with:

detail._get_fringe_value('aqft')

-ak--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#9 Jan. 21, 2011 21:40:22

Trevor S.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Can someone help with Many-to-many referencing and then calculations against another field on the linked model


KennethI have tried your line and I also removed the call to ('qft') on thefringe_value. I now get this error. I'm sure there must be somethingfundamentally wrong with my approach what am I missing!?!Model changed to:

def get_fringe_value(self, fringe):
"""select record by ID from the M2M connection fringes"""ft =Fringe.objects.select_related().filter(id__in=self.fringe.all()).values()"""so as not to pass a single value to sum if only one record
# passed from ft then just pass value of percentage"""
if ft.count()<=1:
qft=ft.filter('percentage')
return u'%d' (qft)"""if more than one record returned from related model do sum onpercentage"""qft=ft(sum('percentage'))"""return value as decimal as it is actually a percentage whichwillbe used to create a new value from currency_total on save"""
return u'%d' (qft)fringe_value = get_fringe_valuedef save(self):
self.native_total = self.quantity*self.multiplier*self.valueself.currency_total =self.quantity*self.multiplier*self.value*self.currency_code.rate1self.variance = self.currency_total-self.prev_total
self.fringe_total = self.fringe_value
super(Detail, self).save()

Error received is:

Request Method: POST
Request URL:http://localhost:8000/admin/bt4/detail/5/Django Version: 1.2.1
Python Version: 2.5.2
Installed Applications:

Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware')


Traceback:File "/usr/lib/python2.5/site-packages/django/core/handlers/base.py" inget_response100. response = callback(request, *callback_args,**callback_kwargs)File "/usr/lib/python2.5/site-packages/django/contrib/admin/options.py"in wrapper239. return self.admin_site.admin_view(view)(*args,**kwargs)File "/usr/lib/python2.5/site-packages/django/utils/decorators.py" in_wrapped_view76. response = view_func(request, *args, **kwargs)File "/usr/lib/python2.5/site-packages/django/views/decorators/cache.py"in _wrapped_view_func69. response = view_func(request, *args, **kwargs)File "/usr/lib/python2.5/site-packages/django/contrib/admin/sites.py" ininner190. return view(request, *args, **kwargs)File "/usr/lib/python2.5/site-packages/django/utils/decorators.py" in_wrapper21. return decorator(bound_func)(*args, **kwargs)File "/usr/lib/python2.5/site-packages/django/utils/decorators.py" in_wrapped_view76. response = view_func(request, *args, **kwargs)File "/usr/lib/python2.5/site-packages/django/utils/decorators.py" inbound_func17. return func(self, *args2, **kwargs2)File "/usr/lib/python2.5/site-packages/django/db/transaction.py" in_commit_on_success299. res = func(*args, **kw)File "/usr/lib/python2.5/site-packages/django/contrib/admin/options.py"in change_view895. self.save_model(request, new_object, form,change=True)File "/usr/lib/python2.5/site-packages/django/contrib/admin/options.py"in save_model597. obj.save()
File "/home/trevor/1stdjangoproject/mysite/../mysite/bt4/models.py" in save
148. super(Detail, self).save()
File "/usr/lib/python2.5/site-packages/django/db/models/base.py" in save435. self.save_base(using=using, force_insert=force_insert,force_update=force_update)File "/usr/lib/python2.5/site-packages/django/db/models/base.py" insave_base501. rows =manager.using(using).filter(pk=pk_val)._update(values)File "/usr/lib/python2.5/site-packages/django/db/models/query.py" in _update
491. return query.get_compiler(self.db).execute_sql(None)File "/usr/lib/python2.5/site-packages/django/db/models/sql/compiler.py"in execute_sql861. cursor = super(SQLUpdateCompiler,self).execute_sql(result_type)File "/usr/lib/python2.5/site-packages/django/db/models/sql/compiler.py"in execute_sql717. sql, params = self.as_sql()File "/usr/lib/python2.5/site-packages/django/db/models/sql/compiler.py"in as_sql826. val = field.get_db_prep_save(val,connection=self.connection)File"/usr/lib/python2.5/site-packages/django/db/models/fields/subclassing.py"in inner28. return func(*args, **kwargs)File"/usr/lib/python2.5/site-packages/django/db/models/fields/__init__.py"in get_db_prep_save775. returnconnection.ops.value_to_db_decimal(self.to_python(value),File"/usr/lib/python2.5/site-packages/django/db/models/fields/__init__.py"in to_python750. return decimal.Decimal(value)
File "/usr/lib/python2.5/decimal.py" in __new__
650. raise TypeError("Cannot convert %r to Decimal" % value)

Exception Type: TypeError at /admin/bt4/detail/5/Exception Value: Cannot convert <bound method Detail.get_fringe_value of<Detail: New Writer for Episode 2 - 1st draft>> to DecimalKenneth Gonsalves wrote:On Fri, 2011-01-21 at 07:33 +0530, Kenneth Gonsalves wrote:On Thu, 2011-01-20 at 19:04 +0000, Trevor Stanley wrote:ft = Fringe.objects.select_related().filter(id=self.id).values()AttributeError: 'str' object has no attribute 'id'maybe filter(id=self.fringe) or (id=self.fringe_id)no - that is wrong. filter(id__in=self.fringe.all())--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

#10 Jan. 22, 2011 00:05:19

Sean W.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Can someone help with Many-to-many referencing and then calculations against another field on the linked model


Hi Trevor,

On Fri, Jan 21, 2011 at 1:47 PM, Trevor Stanley <h...@balancingact.me.uk>wrote:

> def get_fringe_value(self, fringe):


should be

def get_fringe_value(self):

I don't think you are using the local variable "fringe" anywhere in this
method, so you should get rid of it.

and


> self.fringe_total = self.fringe_value
>

should be:

self.fringe_total = self.fringe_value()

I'm not sure if that'll make the whole thing work, but it will fix the error
you received. You have to call the method somewhere along the line or it
won't get executed.

sean

--
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 athttp://groups.google.com/group/django-users?hl=en.

Offline

  • Root
  • » Django
  • » Can someone help with Many-to-many referencing and then calculations against another field on the linked model [RSS Feed]

Board footer

Moderator control

Enjoy the 12th of December
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support