22

How can I get value of course_code in this QuerySet?

<QuerySet [{'course_code': 11}]>

9 Answers 9

21

Use this

someTable.objects.all()[0]['course_code']

or

someTable.objects.values_list('course_code', flat = True)
Sign up to request clarification or add additional context in comments.

2 Comments

someTable.objects.values_list('course_code', flat = True) this returns a queryset.
... not a list*
19
course_qs = <whatever query gave you the queryset>
for course in course_qs:
    print(course['course_code'])

3 Comments

Why am I getting error TypeError: 'DutyModeOfCalc' object is not subscriptable
@user12379095 this is because a queryset return a list, an array. You cannot possibly tell that list to look for index 'course_code', it doesn't exist. You'd have to either loop through it or specify the index in the list, and then for each object you can indicate ['course_code']. It means that this answer is not the correct one
why does queryset not provide any function to get a list? why should I have to loop through every time I fetch a column for example.
10
courses = <your query set>
print(courses[0]['course_code'])

Comments

4

What worked for me:

course_qs = <whatever query gave you the queryset>  
list_of_course = list(course_qs)

When QuerySets are evaluated

  1. The first time you iterate over them
  2. When you slice them, for instance, Post.objects.all()[:3]
  3. When you pickle or cache them
  4. When you call repr() or len() on them
  5. When you explicitly call list() on them
  6. When you test them in a statement, such as bool(), or , and, or if

Comments

4

Now is much easier than before. for instance, you can use:

obj = Model.objects.all().first()  # {'course_code': 11}
course_code = obj.course_code      # 11

Comments

2

Okay this is what i did.

1:

items = ItemParent.objects.filter(item_type__contains="something")
# <QuerySet [<Item: iron>]>

2:

items.values()
# <QuerySet [{'id': 5, 'item_parent_id': 8, 'item_type': 'iron', 'item_article_no': '12336'}]>

3:

items.values()["id"]
# 5

When QuerySets is multiple sets:

# <QuerySet [{'id': 5, 'item_parent_id': 8, 'item_type': 'iron', 'item_article_no': '12336'},
#            {'id': 6, 'item_parent_id': 9, 'item_type': 'rust', 'item_article_no': '12338'}]>

item_id_one = items.values()[0]["id"]
item_id_two = items.values()[1]["id"]
# etc..

Or in my case:

for f in items.values()
    item_id = f["id"]

Comments

1

You can try the following which will return a list of values from the queryset.

courses = <your query set>
linked_content = []
for content in courses:
    linked_content.append(content)
return linked_content

Comments

1

This returns a Query Set: Django 4.0

result = User.objects.filter(Q(ip__icontains='count')) #<QuerySet [<User: count>]>

This returns the value of count:

result.values('count').get()['count']

However this is crazy compilcated?! There must and should be a simpler solution...

Model:

class User(django.Model):
     count = models.PositiveIntegerField(default=1)

Comments

0

You can use this:

>>> course_code = str(table_name.objects.all()[0])
>>> course_code
'11'

Comments

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.