I linking two table
from variety_id to variety_name
but my code is giving an error when the variety_id is null
'NoneType' object is not subscriptable
the code where I am linking table
if not df.empty:
df['commodity_name'] = df.apply(lambda row: commodity_name(row)[0], axis=1)
df['state_name'] = df.apply(lambda row: state_name(row)[0], axis=1)
df['variety_name'] = df.apply(lambda row: variety_name(row)[0], axis=1)
def commodity_name(self):
if self.commodity_id:
return get_commodity_name(self.commodity_id)
return None
def state_name(self):
if self.state_id:
return get_region_name(self.state_id)
return None
def variety_name(self):
if self.variety_id:
return get_variety_name(self.variety_id)
return None
also written query for mapping the columns
def get_variety_name(variety_id):
"""
This function is used to return state_name by querying the database based on lgd_state_id
"""
query = "SELECT commodity_variety_name FROM itrade.commodity_variety_master WHERE commodity_variety_id={}".format(
variety_id)
query_result = get_column_value(query)
if not query_result.empty:
return list(get_column_value(query)['commodity_variety_name'])
return None
def get_commodity_name(commodity_id=None):
"""
This function is used to return commodity_name by querying the database based on commodity_id
"""
query = "SELECT commodity_id, commodity_name FROM itrade.commodity_master"
if commodity_id:
query += " WHERE commodity_id={}".format(commodity_id)
return list(get_column_value(query)['commodity_name'])
return get_column_value(query).to_dict(orient='records')
the DB table is in last row the variety_id is none which is giving the error if I put value in it then the code is working fine
Modify id commodity_id state_id variety_id season_id attribute_value attribute_name attribute_unit status updated_at created_at
edit 1 1 1 1 1 10 base_temp C 1 2021-12-08 05:32:38.565360 2021-11-15 13:53:27.286340
edit 2 2 2 2 2 32 rainfall mm 0 2021-11-15 13:53:27.286340 2021-11-15 13:53:27.286340
edit 3 2 1 5 1 10 temperature Farenhite 1 2021-12-06 08:19:27.754601 2021-12-06 06:58:52.527409
edit 4 2 1 4 12 12 temperature celcius 1 2021-12-06 07:20:36.682156 2021-12-06 07:20:36.682194
edit 5 2 35 5 111 111 temperature Farenhite 1 2021-12-06 08:21:01.312057 2021-12-06 08:20:38.318877
edit 6 2 35 4 2345 1123 temperature (C) celcius 1 2021-12-06 11:36:09.478035 2021-12-06 11:36:01.101701
edit 7 1 1 2 2 10 base_temp C 1 2021-12-17 11:08:49.000000 2021-12-17 11:08:49.000000
edit 8 31 9 60 1 10 base_temp C 1 2021-12-18 09:26:42.000000 2021-12-18 09:26:42.000000
edit 9 84 9 1 2 15 base_temp C 1 2021-12-20 07:48:34.000000 2021-12-20 07:48:34.000000
edit 10 43 9 NULL 111 1111 temperature (F) celcius 1 2021-12-21 12:14:08.384168 2021-12-21 12:14:08.384215
if not df.empty:
df['commodity_name'] = df.apply(lambda row: row.commodity_name()[0], axis=1)
df['state_name'] = df.apply(lambda row: row.state_name()[0], axis=1)
df['variety_name'] = df.apply(lambda row: row.variety_name()[0], axis=1)
response_data = df.to_dict(orient='records')
print(response_data)
if column and column in list(df.columns):
response_data = sorted(response_data, key=lambda i: i[column], reverse=order == 'desc')
if page:
return self.get_paginated_response(self.paginate_queryset(response_data))
return Response(data=response_data, status=status.HTTP_200_OK)
def commodity_name(self, row):
if row.commodity_id:
return get_commodity_name(row.commodity_id)
return None
def state_name(self, row):
if row.state_id:
return get_region_name(row.state_id)
return None
def variety_name(self, row):
if row.variety_id:
return get_variety_name(row.variety_id)
return None
I got the answer thanks for the help