L
L
lolo_to2021-03-31 21:56:39
Django
lolo_to, 2021-03-31 21:56:39

How to sort QuerySet by field ,,OneToOneField,,?

Required: sort the QuerySet by the field of the dependent ,,OneToOneField,, record.

,, parent ,, model

class productModel(models.Model):
    price = models.PositiveIntegerField('Цена', null = True)


dependent model
class characterist_pkModel(models.Model):
    base_product = models.OneToOneField('productModel', on_delete= models.CASCADE)
    category = models.CharField('Категория', max_length= 40, null= True)


And you need to sort the QuerySet with the records of the characterist_pkModel model by the price field of the parent record, like:
characterist_pkModel.objects.get(pk= 1).base_product.order_by('price')

but for the whole QuerySet.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lolo_to, 2021-03-31
@lolo_to

with double underscore (__)
product_objs.order_by('base_product__price')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question