Django list request

I have a database in which there are records with several fields containing some information.

To get all the data in the table corresponding to some filter, I would do this:

records = Record.objects.filter(fieldA='a') 

records, I suppose, is a QuerySet object and contains a "list" of records. It is right?

Now let's say that I need a list of values ​​in one field.

If I do this:

 records = Record.objects.filter(fieldA='a').only('fieldB') 

I still get the request, but now it has some pending fields. I want only a list of values ​​that I would like to capture aka fieldB. I also want to be able to capture the different values ​​of field B. I believe that I could just iterate over each record, pull out field B, add it to the list if it does not already exist, and here it is, but there should be a better way.

Thanks!

EDIT: I think I'm looking

 Record.objects.values_list('fieldB') 
+7
python django
source share
2 answers
+10
source share

I am posting a James comment here to make it more visible. This is definitely what I was looking for.

I need a list of values. Using the QuerySet .list_values() method returns a list of tuples. To get a list of values, I needed the option flat=True .

 Record.objects.values_list('fieldB', flat=True) 
+4
source share

All Articles