Django How to see the session data insted of the object reference

Im trying to start using session in django, Im trying to see the data in my session but im just getting the object reference not the data

console:

<cart.cart.Cart object at 0x7f6994753160>

views.py

def cart_view(request):
    cart = Cart(request)
    print(cart)
    if request.user.is_authenticated:
        return render(request, 'cart/cart_page.html')
    else:
        return redirect('account:login')

cart.py

class Cart:
    def __init__(self, request):

        self.session = request.session
        cart = self.session.get('cart')
        if not cart :
            cart = self.session['cart'] = {}  #cria a sessão cart
        self.product = cart
      

>Solution :

you can override the __str__ method on anyclass to control how it is converted to a string… you can also override the __repr__ method if you need to as well

class Cart:
    def __init__(self, request):

        self.session = request.session
        cart = self.session.get('cart')
        if not cart :
            cart = self.session['cart'] = {}  #cria a sessão cart
        self.product = cart

   def __str__(self):
       return f"<CartObject {self.product}>"

...
print(cart)

Leave a Reply