Reasons why this error is happening.
You will be facing this error (
TypeError: 'flat' is not valid when values_list is called with more than one field
) when you are trying to use
flat=True
with more than one fields in django values_list() function.
values_list
(
*fields
,
flat=False
,
named=False
)
According to Django documentation:
It is an error to pass in flat=True when there is more than one field.
Solution.
When flat=True then you can use only one field in values_list()
# When flat = True
books = Book.objects.all().values_list('id', flat=True)
books
# Output
# <QuerySet [1, 2, 3, 4]>
Read more about values_list() in detail.
Django Queryset To List In Detail
I would greatly appreciate it if you subscribe to my YouTube channel, Let's Code More
Hello, I'm Areeba Seher
Hello ✌️, Areeba Seher here. I'm a Python developer with 2+ years of professional experience in various trending technologies.
I have an interest in Building Full-stack applications , Developing Restful Apis and Building Core backend of web and mobile applications.
Follow me:
Support Us
You may support and appreciate us by buying me a coffee so that we can maintain and expand!
Thank You!
Our goal is to make coding easier and more enjoyable for our readers by providing high-quality materials and valuable tutorials.
Follow Us