0

Below is my raw query using Django Model and data from mysql

    legacy_mapping = {
      'link_desc.id':'link_desc_id',
      'sub_category.name':'sub_category',
      'total_work_days.total_days':'without_recasting_days',
      'total_work_days2.total_days':'with_recasting_days',
    }

    query = '''
      SELECT 
      link_desc.id,
      sub_category.name,
      total_work_days.total_days,
      total_work_days2.total_days
      FROM RSM_LINK_TO_OUTAGE_TYPE_CATEGORY_SUB AS link_desc
      LEFT JOIN RSM_TO_OUTAGE_SUB_CATEGORY AS sub_category ON sub_category.id = link_desc.sub_category_id
      LEFT JOIN RSM_TO_TOTAL_WORK_DAYS AS total_work_days ON total_work_days.id = link_desc.without_recasting_days
      LEFT JOIN RSM_TO_TOTAL_WORK_DAYS AS total_work_days2 ON total_work_days2.id = link_desc.with_recasting_days
      WHERE link_desc.type_category_id=%s;
    '''

    result = RsmLinkToOutageTypeCategorySub.objects.raw(query,[link_id],translations=legacy_mapping)

    columns = result.columns
    print(columns)

    if result is not None:
      for item in result:
        id = getattr(item, columns[0])
        sub_category = getattr(item, columns[1])
        without_recasting_days = getattr(item, columns[2])
        with_recasting_days = getattr(item, columns[3])

        print({
              'sub-category': sub_category,
              'without-recasting-days': without_recasting_days,
              'with-recasting-days': with_recasting_days,
            })

The column prints as ['id', 'name', 'total_days', 'total_days']

So now the result comes as

{'LK_OTCS00001': [
{'sub-category': '66KV Tower SC', 
'without-recasting-days': 12, 
 'with-recasting-days': 12}],

But the answer has to be "8" for 'without-recasting-days'. How do get values based on the translation object?

Deepika Rao
  • 145
  • 1
  • 3
  • 11
  • print(item.without_recasting_days.total_days) - this will give 8 So it has to be legacy-mapping then key of the model – Deepika Rao Jul 11 '23 at 12:06

0 Answers0