subhra
subhra

Reputation: 11

Can not get tables values using Python and Django

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.

Upvotes: 0

Views: 54

Answers (3)

Touhami
Touhami

Reputation: 809

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()

Upvotes: 0

Prakhar Trivedi
Prakhar Trivedi

Reputation: 8526

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.

Upvotes: 2

Ashok Joshi
Ashok Joshi

Reputation: 448

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

Upvotes: 2

Related Questions