well, you're dealing in $$$, so you prob. want floats...but where is your FloatField? an attribute of your class Price? or can you cast your Price class to a float?
pr = 0.0 for a in cart: pr = pr + float(a['choice'].price) do you understand how classes in object oriented languages work? you can only add numbers together, not your own custom classes. (without some extra work i expect - i don't know how operator overloading in python works, or even if it exists) this is what your error statement is telling you...that you're trying to add a number to a non-number class. you have to pull the actual number out of whatever class structure you've put it in. Greg wrote: > Derek, > Ok...I made the change and I'm now getting the error: > > TypeError at /rugs/cart/1/4/ > unsupported operand type(s) for +: 'int' and 'Price' > > Is 'a['choice'].price' not an Int? It says it is in my model file. > > //////////////////////////////////// > > Here is my view > > def showcart(request, style_id, choice_id): > s = Style.objects.get(id=style_id) > c = Choice.objects.get(id=choice_id) > #x = c.size, " ", c.price > cart = request.session.get('cart', []) > cart.append({'style': s, 'choice': c}) > request.session['cart'] = cart > pr = 0 > for a in cart: > pr = pr + a['choice'].price > return render_to_response('show_test.html', {'mychoice': cart, 'p': > pr}) > > > ///////////////// > > Here is my template file: > > {% extends "rug_leftnav.html" %} > {% block body %} > {% for a in mychoice %} > {{ a.style.manufacturer }} - {{ a.style.collection }} - {{ a.style }} > - {{ a.choice.size }} - ${{ a.choice.price }}.00 > <br> > {% endfor %} > {{ p }} > {% endblock %} > > ////////////// > > Here are some of my model classes: > > class Size(models.Model): > name = models.CharField(maxlength=100) > > def __str__(self,): > return self.name > > class Admin: > pass > > class Price(models.Model): > name = models.IntegerField() > > def __str__(self,): > return str(self.name) > > class Admin: > pass > > > class Choice(models.Model): > choice = models.ForeignKey(Collection, edit_inline=models.TABULAR, > num_in_admin=5) > size = models.ForeignKey(Size, core=True) > price = models.ForeignKey(Price, core=True) > def __str__(self,): > return str((self.size, self.price)) > > class Style(models.Model): > name = models.CharField(maxlength=200) > color = models.CharField(maxlength=100) > image = models.ImageField(upload_to='site_media/') > theslug = models.SlugField(prepopulate_from=('name',)) > manufacturer = models.ForeignKey(Manufacturer) > collection = models.ForeignKey(Collection) > sandp = models.ManyToManyField(Choice) > > class Admin: > search_fields = ['name'] > list_filter = ('collection',) > list_display = ('name', 'theslug','collection', 'rmanu') > > js = ( > '/site_media/ajax_yahoo.js', > ) > > def __str__(self,): > return self.name > > def rmanu(self): > return self.collection.manufacturer > > > ////////////////////// > > > Thanks for your help Derek > > > On Jul 19, 4:46 pm, Derek Anderson <[EMAIL PROTECTED]> wrote: >> you were supposed to substitute "value" with whatever column you have >> defined. (you didn't post your model def) >> >> Greg wrote: >>> Derek, >>> I tried that and now I get the following error: >>> AttributeError at /rugs/cart/1/4/ >>> 'Choice' object has no attribute 'value' >>> ///////////// >>> Here is my view >>> def showcart(request, style_id, choice_id): >>> s = Style.objects.get(id=style_id) >>> c = Choice.objects.get(id=choice_id) >>> #x = c.size, " ", c.price >>> cart = request.session.get('cart', []) >>> cart.append({'style': s, 'choice': c}) >>> request.session['cart'] = cart >>> pr = 0 >>> for a in cart: >>> pr = pr + a['choice'].value >>> return render_to_response('show_test.html', {'mychoice': cart, 'p': >>> pr}) >>> ///////// >>> On Jul 19, 4:22 pm, Derek Anderson <[EMAIL PROTECTED]> wrote: >>>> you're not adding two ints, you're adding an int to an instance of your >>>> Choice class. make your line: >>>> pr = pr + a['choice'].value >>>> or whatever you called it. >>>> Greg wrote: >>>>> Hello, >>>>> I have the following view >>>>> def showcart(request, style_id, choice_id): >>>>> s = Style.objects.get(id=style_id) >>>>> c = Choice.objects.get(id=choice_id) >>>>> cart = request.session.get('cart', []) >>>>> cart.append({'style': s, 'choice': c}) >>>>> request.session['cart'] = cart >>>>> pr = 0 >>>>> for a in cart: >>>>> pr = pr + a['choice'] >>>>> return render_to_response('show_test.html', {'mychoice': cart, >>>>> 'p': pr}) >>>>> ///////// >>>>> Whenever i try this I get the error: >>>>> TypeError at /rugs/cart/1/4/ >>>>> unsupported operand type(s) for +: 'int' and 'Choice' >>>>> ////////////////////////// >>>>> Anybody know how I pull a value out of a dict and add it to a existing >>>>> number? >>>>> Thanks > > > > > --~--~---------~--~----~------------~-------~--~----~ 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 [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users?hl=en -~----------~----~----~----~------~----~------~--~---