How to design shopping basket using session?
Asked Answered
A

1

11
class Product(models.Model):
    name = models.CharField(max_length=50)
    slug = models.SlugField()
    unit_price = models.DecimalField(max_digits=5, decimal_places=2)

I'am newbie to Django. How to design shopping basket using session? (ask for a general "algorithm" or some example code)

Aisne answered 19/11, 2012 at 22:24 Comment(3)
Are you asking about DB design or something else? Also you could take some ideas from here github.com/ahmet/django-cartHamner
I ask for db design and how to store product in sessionAisne
You probably need to store the cart in session and not the product. The link I gave you features very simple code patterns. I suggest you take a look at it. Also, in order to design you DB, you have to make up your mind about the features you need. You question is quite vague at this point.Hamner
M
15

I wouldn't use a model. You can store the values directly in the session. Considering that you can store everything in the session you can store the items in a dictionary do something like.

def view_cart(request):
    cart = request.session.get('cart', {})
    # rest of the view

def add_to_cart(request, item_id, quantity):
    cart = request.session.get('cart', {})
    cart[item_id] = quantity
    request.session['cart'] = cart
    # rest of the view
Militarist answered 20/11, 2012 at 1:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.