User100696
User100696

Reputation: 707

Django - How can I print a record by console?

I have 2 records in my database then I'm trying to print it like that:

instance = People.objects.all()
for item in instance:
    qs_name = item.name
    qs_user = item.user
    qs_password = item.password
    print qs_name,qs_user,qs_password

I'm getting something like this:

Kevin kjhf98 896687

Joe jwd22 336558

I want to store in a variable a single record, is that even possible?

for example:

Person_1 = Kevin kjhf98 896687

then if I print Person_1 I'll get only

Kevin kjhf98 896687

I need this for something else that's why I'm asking for help..

any help would be apreciate!! thanks!

Upvotes: 1

Views: 5914

Answers (2)

neverwalkaloner
neverwalkaloner

Reputation: 47364

You can use values_list:

instance = People.objects.all().values_list('name', 'user__username', 'password')

for item in instance:
    print(item)

output:

(Kevin, kjhf98, 896687)
(Joe, jwd22, 336558)

or if you want just string:

for item in instance:
    print(' '.join(str(i) for i in item))

output:

Kevin kjhf98 896687
Joe jwd22 336558    

Upvotes: 3

Bipul Jain
Bipul Jain

Reputation: 4643

Yes, i suppose you can do this.

Person_1 = "{} {} {}".format(item.name,item.user,item.password)

Upvotes: 1

Related Questions