I am trying to use bulk import, to import data from a csv file and populate an inherited model from a separate module, I was able to get the data and loop through all the data in the csv file by checking for the csv file path reading the datas from the file and looping through each rows, now I am trying to insert the gotten data to the database using graphql and Django framework, I need the data to populate an inherited module model table called insuree, using the below code, i am unable to send the data to database which is stopping the table from populating.
Here is my mutation code
import graphene
from core import prefix_filterset, ExtendedConnection, filter_validity, Q, assert_string_length
from core.schema import TinyInt, SmallInt, OpenIMISMutation, OrderedDjangoFilterConnectionField
from insuree import models as insuree_models
from django.contrib.auth.models import AnonymousUser
from django.core.exceptions import ValidationError, PermissionDenied
from django.utils.translation import gettext as _
from graphene import InputObjectType
import csv
class BulkImportInputType(OpenIMISMutation.Input):
file_path =graphene.String(required=True)
def set_import_insuree_data(self, data, user):
data_list = []
col_list = []
all_data = []
file_path = data['file_path']
with open(file_path, newline='') as csvfile:
data = csv.reader(csvfile, delimiter=' ', quotechar='|')
for row in data:
data_list.append(row)
for col in data_list[0]:
for column in col.split(','):
col_list.append(column)
data_list.pop(0)
for row in data_list:
r=[]
for rr in row[0].split(','):
r.append(rr)
vals={
'SRNumber' : r[col_list.index('SRNumber')],
'PermanentVillageCode' : r[col_list.index('PermanentVillageCode')],
'Education' : r[col_list.index('Education')],
'LastName' : r[col_list.index('LastName')],
'BirthDate' : r[col_list.index('BirthDate')],
'Gender' : r[col_list.index('Gender')],
'PovertyStatus' : r[col_list.index('PovertyStatus')],
'ConfirmationNo' : r[col_list.index('ConfirmationNo')],
'ConfirmationType' : r[col_list.index('ConfirmationType')],
'PermanentAddress' : r[col_list.index('PermanentAddress')],
'MaritalStatus' : r[col_list.index('MaritalStatus')],
'CurrentVillageCode' : r[col_list.index('CurrentVillageCode')],
'CurrentAddress' : r[col_list.index('CurrentAddress')],
'Proffesion' : r[col_list.index('Proffesion')],
'OtherName' : r[col_list.index('OtherName')]
}
all_data.append(vals)
print(all_data)
return all_data
class CreateBulkImportInsureeMutation(OpenIMISMutation):
_mutation_module = "Insuree"
_mutation_class = "CreateBulkImportInsureeMutation"
class Input(BulkImportInputType):
@classmethod
def async_mutate(cls, user, **data):
try:
all_data= set_import_insuree_data(data, user)
for insuree_data in all_data:
insuree_data['audit_user_id'] = user.id_for_audit
from core.utils import TimeUtils
insuree_data['validity_from'] = TimeUtils.now()
# insuree = CreateInsureeMutation.async_mutate(user,insuree_data)
return None
except Exception as exc:
return [{
'message': "Bulk import of insuree failed",
'detail': str(exc)}]
and here is my schema.py
class Mutation(graphene.ObjectType):
uploadcsv = CreateBulkImportInsureeMutation.Field()