0

When an output is received in the below form after running the query

<QuerySet [<key: value object (ad5bb6b4-8035)>]> 

I want to get ad5bb6b4-8035 string only for further operations.

So I tried

course_qs =  <QuerySet [<key: value object (ad5bb6b4-8035)>]>
for course in course_qs:
    print(course)

which returned

value object (ad5bb6b4-8035)

How to get only ad5bb6b4-8035.?

getting values of QuerySet in django How to extract numerical value from Django queryset? Extracting message from django queryset

4
  • It's not the object.id? Commented Sep 23, 2022 at 0:06
  • You can also see all the object value under its dict attribute. See if its in there Commented Sep 23, 2022 at 0:08
  • @GabrielBoehme Yes its an object id Commented Sep 23, 2022 at 0:09
  • @GabrielBoehme course.id solved the issue. Thanks! Commented Sep 23, 2022 at 0:11

1 Answer 1

0

values() is extract key,value for the specific column from queryset it's return list of dictionaries.

value1 = User.objects.all().values('username')
    
    #output = <QuerySet [{'username': 'one'}, {'username': 'three'}, {'username': 'two'}]>

values_list() is extract only values for the specific column from queryset it's return list of tuples.

value2 = User.objects.all().values_list('username')

#output = <QuerySet [('one',), ('three',), ('two',)]>
Sign up to request clarification or add additional context in comments.

2 Comments

Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes.
I explained in answer check it out.

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.