I have a project built with Django Rest Framework. In one of the views, I'm implementing a functionality that takes a CSV file sent from the frontend (React.js), and that creates model instances from the rows of the CSV file.
This is the model that I'm trying to upload:
class ModelExample(models.Model):
name = models.CharField(...)
logo = models.ImageField(...)
.
.
.
This is the code that I'm using to create the model instances:
class ModelExampleCreateAPIView(APIView):
def get(self, request, format=None):
.
.
.
modelExampleList = list()
reader = pd.read_csv(file)
for rowIndex, row in reader.iterrows():
newModelExample = ModelExample(
name=row['Name'],
# logo = row['Logo']
)
modelExampleList.append(newModelExample)
ModelExample.objects.bulk_create(modelExampleList)
.
.
.
This is an example of the CSV file that I'm using:
Name | Logo |
---|---|
Name Example 1 | https://url-to-logo-1 |
Name Example 2 | https://url-to-logo-2 |
My question is how can I assign the logo column to the model instances. The solutions I have found so far force to save the model instances inside of the loop (in the view) instead of using bulk_create. Since I'm creating a considerable amount of model instances at once, I need to use the bulk_create instead of saving one by one.
All suggestions are very appreciated
These are some of the links I have found:
- Programmatically saving image to Django ImageField
- Django: add image in an ImageField from image url
Note: I do not want to save the url but the image from the url itself.