In short i am doing online store and in django admin i display authenticated users 'username' but there are users which didn't authenticated
So if authenticated users will order something it's okay i have their data. But if unauthenticated users will order? What to show in django admin? If i will request from user only his name from this no result. Because we know django need full authorization
models.py
class Cart(models.Model):
user = models.ForeignKey(User, null=True, blank=True)
products = models.ManyToManyField(Product, through=CartProduct)
class Meta:
verbose_name = 'Cart'
verbose_name_plural = 'Cart'
def __str__(self):
return self.user.username # That for authenticated users
views.py
class CartView(View):
def get(self,request,*args,**kwargs):
cart_id = request.session.get('cart_id')
if cart_id == None:
cart = Cart()
cart.save()
request.session['cart_id'] = cart_id
cart = Cart.objects.get(id=cart_id)
product_id = request.GET.get('product')
delete_product = request.GET.get('delete')
if product_id:
product_instance = get_object_or_404(Product, id=product_id)
amount = request.GET.get('amount')
cart_product = CartProduct.objects.get_or_create(cart=cart, product=product_instance)[0]
if delete_product:
cart_product.delete()
else:
cart_product.amount = amount
cart_product.save()
return HttpResponseRedirect('/')