How to convert Queryset to String?

4,526 views
Skip to first unread message

Joshua O. Morales

unread,
Jan 2, 2018, 4:30:54 AM1/2/18
to Django users
title = Title.objects.filter(title=title)
print(title)
Is there a way to turn this:
<QuerySet[<Title: Hello, World>]>
into a string:
Hello, World



I tried using it as an argument but it gave me an error.

Etienne Robillard

unread,
Jan 2, 2018, 4:34:44 AM1/2/18
to Joshua O. Morales, django...@googlegroups.com

if your title object has a __str__ then it can be printed.

Printing a queryset object make no sense!

HTH

Etienne

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/9d47f644-68f6-4939-a0f2-65b4681701b5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

-- 
Etienne Robillard
tka...@yandex.com
https://www.isotopesoftware.ca/

pradam

unread,
Jan 2, 2018, 4:39:12 AM1/2/18
to django...@googlegroups.com
get_object = get_object_or_404(Title, title=title)
if get_object:
   print(get_object.title)
or

title = Title.objects.filter(title=title)
if title:
   print(title[0].title)

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.

Joshua O. Morales

unread,
Jan 2, 2018, 4:42:08 AM1/2/18
to Django users
Here's the snippet of my code:
models.py:
class Comment(models.Model):
comment_title = models.TextField()
comment_content = models.TextField()
def __str__(self):
return self.comment_content



def test_me():
comment_id_set = list(Comment.objects.values_list('id', flat=True))
for pk in comment_id_set:
comment = Comment.objects.filter(pk=pk)
print(comment)

Output:
<QuerySet [<Comment: Nice weather and location. Not much food choices. The room was fair but clean. Not really fancy. The staff was friendly and helpful. :)>]>
<QuerySet [<Comment: I booked this room for my brother as a gift. They really enjoyed the place.>]>

Daniel Roseman

unread,
Jan 2, 2018, 4:52:30 AM1/2/18
to Django users
On Tuesday, 2 January 2018 09:42:08 UTC, Joshua O. Morales wrote:
Here's the snippet of my code:
models.py:
class Comment(models.Model):
comment_title = models.TextField()
comment_content = models.TextField()
def __str__(self):
return self.comment_content



def test_me():
comment_id_set = list(Comment.objects.values_list('id', flat=True))
for pk in comment_id_set:
comment = Comment.objects.filter(pk=pk)
print(comment)

Output:
<QuerySet [<Comment: Nice weather and location. Not much food choices. The room was fair but clean. Not really fancy. The staff was friendly and helpful. :)>]>
<QuerySet [<Comment: I booked this room for my brother as a gift. They really enjoyed the place.>]>



But your code makes no sense. You do a query to get all the IDs in the database, then for each ID you do yet another query for the ID - using `filter` which returns a queryset, rather than `get` which returns an object - and then print that queryset. Why?

Your code can be simplified to:

comments  = Comment.objects.all()
for comment in comments:
    print comment

--
DR.
Reply all
Reply to author
Forward
0 new messages