Tuesday, January 2, 2018

Re: How to convert Queryset to String?

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.>]>


On Tuesday, January 2, 2018 at 5:34:44 PM UTC+8, Etienne Robillard wrote:

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

Printing a queryset object make no sense!

HTH

Etienne


Le 2018-01-02 à 04:30, Joshua O. Morales a écrit :
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.

--
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/

--
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.
To post to this group, send email to django-users@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/b0336f03-63ff-4a1e-9138-056d5a8c9cbe%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment