Reputation: 125
I've been stuck for a while trying to figure why Django throws this AttributeError
when attempting to add an item to a basket.
The error looks like this:
AttributeError: 'Basket' object has no attribute 'save'
I've scoured over my code but can't seem to find the issue. I've posted the necessary code below.
basket/basket.py:
class Basket():
def __init__(self, request):
self.session = request.session
basket = self.session.get('skey')
if 'skey' not in request.session:
basket = self.session['skey'] = {}
self.basket = basket
def add(self, product, qty):
"""
Adding and updating the users basket session data
"""
product_id = str(product.id)
if product_id in self.basket:
self.basket[product_id]['qty'] = qty
else:
self.basket[product_id] = {'price': str(product.price), 'qty': qty}
self.save()
def __iter__(self):
"""
Collect the product_id in the session data to query the database
and return products
"""
product_ids = self.basket.keys()
products = Product.products.filter(id__in=product_ids)
basket = self.basket.copy()
for product in products:
basket[str(product.id)]['product'] = product
for item in basket.values():
item['price'] = Decimal(item['price'])
item['total_price'] = item['price'] * item['qty']
yield item
def __len__(self):
"""
Get the basket data and count the qty of items
"""
return sum(item['qty'] for item in self.basket.values())
basket/views.py:
def basket_summary(request):
basket = Basket(request)
return render(request, 'main_store/basket/summary.html', {'basket': basket})
def basket_add(request):
basket = Basket(request)
if request.POST.get('action') == 'post':
product_id = int(request.POST.get('productid'))
product_qty = int(request.POST.get('productqty'))
product = get_object_or_404(Product, id=product_id)
basket.add(product=product, qty=product_qty)
basketqty = basket.__len__()
response = JsonResponse({'qty': basketqty})
return response
script to add product - single_product.html:
<script>
$(document).on('click', '#add-button', function (e) {
e.preventDefault();
$.ajax({
type: 'POST',
url: '{% url "basket:basket_add" %}',
data: {
productid: $('#add-button').val(),
productqty: $('#select option:selected').text(),
csrfmiddlewaretoken: "{{csrf_token}}",
action: 'post'
},
success: function (json) {
document.getElementById("basket-qty").innerHTML = json.qty
},
error: function (xhr, errmsg, err) { }
});
})
</script>
At first, I thought it may be a simple case of having something named as basket
instead of the capitalized Basket
. But I think the issue may be deeper than that.
I'd really appreciate any help and if any more info is needed please let me know and i'll be happy to provide it. Thanks!
Upvotes: 0
Views: 221
Reputation: 66
The add
method of your Basket class contains the following line:
self.save()
However, the Basket class currently has no implementation of a method save
. It doesn't inherit it from any parent class either.
The fact that the add
method attempts to execute a class method that is undefined explains your attribute error.
Upvotes: 1