Let’s say you have the following Django ORM query:
cars = CarModel.objects.only(
'car__car_id',
'car__cardate',
'car__title',
'car__location',
'car__quote',
'car__key',
'car__cartype_id',
'maker__lastname',
'maker__firstname',
'maker__middlename',
'maker__nickname',
).select_related(
'car',
'maker',
).extra(
select={
'is_position_paper': 'cartype_id = 7',
'is_null_date': 'cardate IS NULL',
'shorttitle': extra,
},
).filter(**kwargs).distinct(sort_order_for_distinct, 'car__car_id').order_by(sort_order, 'car__car_id')
My understanding is that, whatever I’ve included in order_by() I have to also include, in the same order, in distinct(). However, distinct doesn’t like the attributes being created in extra(). If I try and include these attributes in the order_by() as I would like, such as:
.order_by('is_position_paper', 'is_null_date', sort_order, 'car__car_id')
I get the error:
Cannot resolve keyword ‘is_position_paper’ into field
So that means that the attributes created via extra() just aren’t going to be available to use with distinct()? Is there any alternative approach here?
Really all I’m trying to do is dedupe this entire queryset by car__car_id, that’s it, whilst having order_by('is_position_paper', 'is_null_date', sort_order, 'car__car_id')
Unfortunately I’m stuck on Django 2.1 with this. Also, using Postgres as the db.