Re: How to loop through a Dict to add values?
"del( some_dict[id] )", if you have a dict. but it looks here like you have a list containing a dict... i highly recommend buying a python booki liked the "Python Essential Reference" by David M. Beazley when i was first learning python (still am, actually), if you're already comfortable with programming in general. (if not, something else - it's not an intro-to-oop book) http://www.amazon.com/Python-Essential-Reference-Developers-Library/dp/0672328623/ref=sr_1_1/105-2347441-5534819?ie=UTF8&s=books&qid=1184942097&sr=8-1 Greg wrote: > Ok...on to a new problem. I'm able to add stuff to my session and > also delete the entire sesssion. However, I'm not sure how to delete > a particular entry into my session variable. This is how i add stuff > to my session > > cart = request.session.get('cart', []) > cart.append({'style': s, 'choice': c}) > > What method do I use on request.session to delete only one "row" in my > cart dictionary? Does cart have an id field that I can access? > > Thanks > > On Jul 20, 8:44 am, Greg <[EMAIL PROTECTED]> wrote: >> Yep...that worked. Thanks for your help Nis and Derek. I'll try to >> modify my model file with your suggestions >> >> On Jul 20, 5:46 am, Nis Jørgensen <[EMAIL PROTECTED]> wrote: >> >>> Greg skrev:> 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. >>> No it doesn't - "price" is defined as a ForeignKey to Price, so it holds >>> a Price object. 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)) >>> The only integer defined is Price.name. Does this work: >>> pr = pr + a['choice'].price.name >>> ? >>> I would suggest that you change the name of that field ... "name" does >>> not really convey that this is the value of the price - it sounds more >>> like a label for it (like "SuperSaver"). >>> Perhaps you could get rid of the Price class altogether? If it just >>> holds an integer, you could use an IntegerField directly in Choice ... >>> Nis > > > > > --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: How to loop through a Dict to add values?
Ok...on to a new problem. I'm able to add stuff to my session and also delete the entire sesssion. However, I'm not sure how to delete a particular entry into my session variable. This is how i add stuff to my session cart = request.session.get('cart', []) cart.append({'style': s, 'choice': c}) What method do I use on request.session to delete only one "row" in my cart dictionary? Does cart have an id field that I can access? Thanks On Jul 20, 8:44 am, Greg <[EMAIL PROTECTED]> wrote: > Yep...that worked. Thanks for your help Nis and Derek. I'll try to > modify my model file with your suggestions > > On Jul 20, 5:46 am, Nis Jørgensen <[EMAIL PROTECTED]> wrote: > > > Greg skrev:> 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. > > > No it doesn't - "price" is defined as a ForeignKey to Price, so it holds > > a Price object. > > > > 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)) > > > The only integer defined is Price.name. Does this work: > > > pr = pr + a['choice'].price.name > > > ? > > > I would suggest that you change the name of that field ... "name" does > > not really convey that this is the value of the price - it sounds more > > like a label for it (like "SuperSaver"). > > > Perhaps you could get rid of the Price class altogether? If it just > > holds an integer, you could use an IntegerField directly in Choice ... > > > Nis --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: How to loop through a Dict to add values?
Yep...that worked. Thanks for your help Nis and Derek. I'll try to modify my model file with your suggestions On Jul 20, 5:46 am, Nis Jørgensen <[EMAIL PROTECTED]> wrote: > Greg skrev:> 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. > > No it doesn't - "price" is defined as a ForeignKey to Price, so it holds > a Price object. > > > > > 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)) > > The only integer defined is Price.name. Does this work: > > pr = pr + a['choice'].price.name > > ? > > I would suggest that you change the name of that field ... "name" does > not really convey that this is the value of the price - it sounds more > like a label for it (like "SuperSaver"). > > Perhaps you could get rid of the Price class altogether? If it just > holds an integer, you could use an IntegerField directly in Choice ... > > Nis --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: How to loop through a Dict to add values?
Greg skrev: > 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. > No it doesn't - "price" is defined as a ForeignKey to Price, so it holds a Price object. > 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)) > The only integer defined is Price.name. Does this work: pr = pr + a['choice'].price.name ? I would suggest that you change the name of that field ... "name" does not really convey that this is the value of the price - it sounds more like a label for it (like "SuperSaver"). Perhaps you could get rid of the Price class altogether? If it just holds an integer, you could use an IntegerField directly in Choice ... Nis --~--~-~--~~~---~--~~ 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 -~--~~~~--~~--~--~---
Re: How to loop through a Dict to add values?
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 > > {% 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}) > / > When
Re: How to loop through a Dict to add values?
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 {% 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 -~--~~~~--~~--~--~---
Re: How to loop through a Dict to add values?
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 -~--~~~~--~~--~--~---
Re: How to loop through a Dict to add values?
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 -~--~~~~--~~--~--~---
Re: How to loop through a Dict to add values?
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 -~--~~~~--~~--~--~---
How to loop through a Dict to add values?
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 -~--~~~~--~~--~--~---