Добавить корзину на django-shopping-cart (ошибка объект 'str' не имеет атрибута 'id')
Я использую django-shopping-cart 0.1 для добавления тележек, но здесь данные поступают из api, поэтому я не создавал модель продукта. При попытке добавить у меня возникает ошибка: ('str' объект не имеет атрибута 'id' ) эта ошибка происходит из модуля cart.py в django-shopping при: id = product.id Напомню, что на уровне API, который извлекает продукты, у меня есть поля (код, обозначение)
поэтому я передаю поле 'code' в качестве параметра для добавления корзины, так как там нет 'id'
View.py
@login_required
def cart_add(request,code):
url='http://myapi/Article/GetArticle'
x=requests.get(url)
content=x.json()
articles=content['articles']
for article in articles :
if article.get('code') == code :
cart=Cart(request)
myarticle=article.get('code')
cart.add(product=myarticle)
return render(request,'cart/deatil.html')
Модуль Cart.py
class Cart(object):
def __init__(self, request):
self.request = request
self.session = request.session
cart = self.session.get(settings.CART_SESSION_ID)
if not cart:
# save an empty cart in the session
cart = self.session[settings.CART_SESSION_ID] = {}
self.cart = cart
def add(self, product, quantity=1, action=None):
"""
Add a product to the cart or update its quantity.
"""
id = product.id
newItem = True
if str(product.id) not in self.cart.keys():
self.cart[product.id] = {
'userid': self.request.user.id,
'product_id': id,
'name': product.name,
'quantity': 1,
'price': str(product.price),
'image': product.image.url
}
else:
newItem = True
for key, value in self.cart.items():
if key == str(product.id):
value['quantity'] = value['quantity'] + 1
newItem = False
self.save()
break
if newItem == True:
self.cart[product.id] = {
'userid': self.request,
'product_id': product.id,
'name': product.name,
'quantity': 1,
'price': str(product.price),
'image': product.image.url
}
self.save()
документация api у нас есть:
{
"articles": [
{
"code": "string",
"designation": "string"
}
]
}