0

I am facing one issue. I am trying to fetch my tables values using the id in python but its not coming at all. I am explaining my code below.

 pers = User.objects.get(pk=request.session['id'])
    root = []
    print(pers)
    user_name = pers.uname
    count = 1
    root.append(
        {'username': user_name,
             })
    return render(request, 'bookingservice/home.html', {'user': root, 'count': 1})

Here I am printing the pers value but its showing User object. Here I need to fetch the row values as per id present inside session. Please help me.

3
  • What is the error you are facing? Hope it is not the identation problem, as your indentation is wrong here. Commented Jul 10, 2017 at 5:48
  • Are you looking for the currently logged in user? request.user gives you the User object. OR does requset.session['id'] hold an id to some other user? Commented Jul 10, 2017 at 5:56
  • Is my answer helpful?? Commented Jul 10, 2017 at 9:07

3 Answers 3

2

As I have seen your code, it is perfectly fine. When you print 'pers', it should print User object because get always returns an object of the model. If you want to print all the fields values, you should try pers.values().

and as per your code you are already getting all the fields like pers.uname

Sign up to request clarification or add additional context in comments.

Comments

2

On printing pers value, it is showing User object because the get query is returning Object of User Model based on pk = request.session['id']. Inside pers object, there will be fields as defined in the User Model. You can access them as:

user_name = pers.username
print (user_name), "username"

OR

print (pers.username), "pers.username"
print (pers.first_name), "pers.first_name"
print (pers.values()), "pers.values()"

And so on.

Comments

0

Django queryset is an ORM so all your responses are objets ans not arrays, here your request return an Object and it's fine,

so you can access to cols by names, example : pers.uname

if you want to get all values use

pers.values()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.