I am trying to create new instances in my Django model using bulk_create().
My models are:
Class Emotion(models.Model):
emotion_grp_id = models.AutoField(primary_key=True, ...
emotion_group = models.CharField(max_length=55, ...
Class EmotionSubGroup(models.Model):
emotion_sub_grp_id = models.AutoField(primary_key=True, ...
emotion_grp = models.ForeignKey(EmotionGroup, on_delete=models.CASCADE, ...
emotion_sub_group = models.CharField(max_length=55, ...
views.py
The relevant portion of the function that I am using looks like this:
def DataUpload (request):
# ...
# ...
df_rawdata = pd.read_csv(csv_file, sep=''). # Dataframe from source (.csv) file
row_iter = df_rawdata.iterrows()
data = [
EmotionSubGroup(
emotion_grp_id=row['emotion_grp'],
emotion_sub_group=row['emotion_sub_group'],
)
for index, row in row_iter
]
EmotionSubGroup.objects.bulk_create(data)
Is it possible to create a general data structure instead of hardcoding the field names, for example:
data = `list_of_target_fields = rows_from_external_source`
to be equivalent of what I am using currently and carry out the upload.
Here is a simple function which will update
ForeignKey
and will return a instance of modelOther approch to make code readable