[Answered ]-Iterator should return strings, not bytes (the file should be opened in text mode)

1👍

You can read the data of the uploaded file as follows and the problem will be solved.

new_person = request.FILES['myfile'].read().decode("utf-8")

According to the model you don’t have an ID, just make the following changes to log in

for data in imported_data:
  value = Person(
    data[1],
    int(data[2])
  )
 value.save()

But if you want to have an ID in the model, then you must change the model as follows and write the loop as follows (of course, you must have values in the ID column in the CSV file)

class Person(models.Model):
  id      = models.IntegerField(primary_key=True)
  name    = models.CharField(max_length=200)
  marks   = models.IntegerField()
  def __str__(self):
    return self.name

loop try this

for data in imported_data:
  value = Person(
    int(data[0]),
    data[1],
    int(data[2])
  )
 value.save()

Leave a comment