collerek/ormar

`values_list` querying and returning wrong flattened data

Opened this issue · 0 comments

When using values_list through a relationship ormar is return all non-nullable fields and the taking the first one as the flattened value, which is the wrong field/values.

An example, say I have a list of car ids, and I want to find the set of owners for that set of cars. Each car has a garage, each garage has an owner. I only want the owner_id's for efficiency sake.

car_ids = [1, 2, 3]

await Cars.objects.select_related([Car.garage])
    .filter(id__in=car_ids)
    .values_list("garage__owner", flatten=True)

Car has a bunch of non-nullable fields as does Garage and Owner, hence why I want to use values_list to query raw data and not hydrate actual models

When this is executed I actually get the whole of the Car and Garage models queried for (I can see the SELECTs for these columns in the SQL), and then https://github.com/collerek/ormar/blob/master/ormar/queryset/queryset.py#L640 is just grabbing the first value, which happens to be the Car.id not anything to do with Car.garage.owner.id.

I've tried using fields_exclude to explicitly remove non-nullable fields but it doesn't work. I've tried a combination of flatten and exclude_through with similar results.

As far as I can tell, using values_list isn't allowing me to bypass non-nullable fields? Is there something I've missed, or not understood about querying raw data like this?