I want to get only customer id but getting whole customer information

Viewed 134

I am trying to show orders by customer id by i am getting this error :

TypeError at /orders
Field 'id' expected a number but got {'id': 3, 'phone_number': '01622153196', 'email': 'sakibovi@gmail.com', 'password': 'pbkdf2_sha256$216000$H2o5Do81kxI0$2tmMwSnSJHBVBTU9tQ8/tkN7h1ZQpRKrTAKkax1xp2Y=', 'coin': 1200.0}.

Actually i want to fetc only customer id but getting whole dictionary.

Here in Login Class in views.py i fetch whole customers info like this

request.session['customer'] = customer.__dict__

Here is the details :

class Login(View):
    def get(self, request):
        return render(request, 'signupsignin/signin.html')



    def post(self, request):
        phone_number = request.POST.get('phone_number')
        password = request.POST.get('password')
        customer = Customer.get_customer(phone_number)
        error_message = None
        if customer:
            match = check_password(password, customer.password)
            if match:
                customer.__dict__.pop('_state')
                request.session['customer'] = customer.__dict__
                # request.session['customer'] = customer.id
                #request.session['customer'] = customer.coin
                #request.session['phone_number'] = customer.phone_number
                return redirect('Home')
            else:
                error_message = 'Phone number or Password didnt match on our record'
        else:
            error_message = 'No Customer Found! Please Registrer First!'
        print(phone_number, password)
        context = {'error_message':error_message}
        return render(request, 'signupsignin/signin.html', context)

I think for that reason i am getting the whole informaton of a customer

Here is my Views.py for userorders by customer id ::

class UserOrders(View):
    def get(self, request):
        customer = request.session.get('customer')
        user_orders = Order.get_orders_by_customer(customer)
        print(user_orders)
        args = {'user_orders':user_orders}
        return render(self.request, 'Home/all_orders.html', args)

Here i have a method named get_orders_by_customer() i made this in models.py Here it is ::

@staticmethod
    def get_orders_by_customer(customer__id):
        return Order.objects.filter(customer=customer__id)

So what i am trying to do is customers can see their own orders.I have a panel called "all orders" here a customer can see their own order only.

Please Help me i got really confused here

2 Answers

As per my understanding, you have to pass a number, but you're passing a whole dictionary.

@staticmethod def get_orders_by_customer(customer__id): return Order.objects.filter(customer=customer__id)

here before return try to debug with a print or something. and you'll see what I mean.

try this and it should work, if im not wrong: costomer__id['id'] instead of costomer__id

or change your code into this:

@staticmethod
def get_orders_by_customer(customer):
    return Order.objects.filter(customer=customer['id'])
Related