def attach_basic_attachments(queryset, as_field='attachments_attr'):
 model = queryset.model
 type = apps.get_model('contenttypes', 'ContentType').objects.get_for_model(model)
 sql = 'SELECT json_agg(row_to_json(t))\n                FROM(\n                    SELECT\n                        attachments_attachment.id,\n                        attachments_attachment.attached_file\n                    FROM attachments_attachment\n                    WHERE attachments_attachment.object_id = {tbl}.id\n                     AND  attachments_attachment.content_type_id = {type_id}\n                     AND  attachments_attachment.is_deprecated = False\n                    ORDER BY attachments_attachment.order, attachments_attachment.created_date, attachments_attachment.id) t'
 sql = sql.format(tbl=model._meta.db_table, type_id=type.id)
 queryset = queryset.extra(select={as_field: sql})
 return queryset

def attach_basic_attachments ( queryset , as_field = 'attachments_attr' ) : model = queryset . model type = apps . get_model ( 'contenttypes' , 'ContentType' ) . objects . get_for_model ( model ) sql = 'SELECT json_agg(row_to_json(t))\n                FROM(\n                    SELECT\n                        attachments_attachment.id,\n                        attachments_attachment.attached_file\n                    FROM attachments_attachment\n                    WHERE attachments_attachment.object_id = {tbl}.id\n                     AND  attachments_attachment.content_type_id = {type_id}\n                     AND  attachments_attachment.is_deprecated = False\n                    ORDER BY attachments_attachment.order, attachments_attachment.created_date, attachments_attachment.id) t' sql = sql . format ( tbl = model . _meta . db_table , type_id = type . id ) queryset = queryset . extra ( select = { as_field : sql } ) return queryset
attach basic attachments info as json column to each object of the queryset .