Ordering a request for filtered child objects

I have the following (simplified) data model, see the visual below of the post:

  • Articles with attributes
  • Attributes refer to PK to a type that has a field code
  • Attributes have field value
  • The value refers to the uuid field in another model called "Record" which also has a sorting_code field

Now I want to return a list of articles in a specific order using pagination. I am using the default ViewSet for it. Paging forces me to place an order in the database, not later in Python. However, I cannot find the correct offer ordering

that orders these articles. I need to do the following:

  • Get an attribute of a specific type
  • Look at the values โ€‹โ€‹in these attributes in the record table
  • Order articles based on sorting_code

The following SQL query does the job (for all articles):

SELECT art.order_id, art.uuid, att.value, mdr.code, mdr.name, mdr.sorting_code
FROM ow_order_article art
INNER JOIN ow_order_attribute att ON att.article_id = art.uuid
INNER JOIN ow_masterdata_type mdt ON att.masterdata_type_id = mdt.uuid
INNER JOIN ow_masterdata_record mdr ON att.value = mdr.uuid
WHERE mdt.code = 'article_structure'
ORDER BY mdr.sorting_code, mdr.code

      

What would be the correct way to get this ordering in a queryset in Django?

simplified data model

+3


source to share





All Articles