title: IMPORTING CSV DATAS INTO MODEL - DJANGO
body: <p>I'm implementing a list of clients. I want to give to the user the possibility of importing new clients thought a csv file.
  The client model has these fields: Client, name, surname, email, phone</p>
  <p>So I created this model:</p>
  <pre><code>class CsvClient(models.Model):
  file_name = models.FileField(upload_to='csv-cliente')
  uploaded = models.DateTimeField(auto_now_add=True)
  activated = models.BooleanField(default=False)
  
  def __str__(self):
      return f&quot;File id: {self.id}&quot;
  </code></pre>
  <p>and this function in views.py:</p>
  <pre><code>import csv
  
  def importa_csv_clienti(request):
  form = CVSForm(request.POST or None, request.FILES or None)
  
  if form.is_valid():
      form.save()
      form = CVSForm()
      clients = CsvClient.objects.get(activated=False)
      with open(clienti.file_name.path, 'r') as f:
          reader = csv.reader(f)
  
          for i, row in enumerate(reader):
              if i==0:
                  pass
              else:
                  row = &quot;&quot;.join(row)
                  row = row.replace(&quot;;&quot;, &quot; &quot;)
                  row = row.split(&quot; &quot;)
  
                  client = row[0].capitalize()
                  name = row[1].capitalize()
                  surname = row[2].capitalize()
  
                  value = Cliente.objects.create(
                      cliente=cliente,
                      nome=nome,
                      cognome=cognome,
                      email=riga[3],
                      telefono=riga[4],
                  )
  
                  print('oggetto creato:', value.cliente, value.nome, value.cognome, value.email, value.telefono)
  
          clients.activated = True
          clients.save()
  
  context = {'form': form}
  template = 'importa.html'
  return render(request, template, context)
  </code></pre>
  <p>It works, expect for the fact that if in the csv file I have the row:</p>
  <p>Nutella Antonio Dello Iudice</p>
  <p>where
  nutella is client
  Antonio is name
  Dello Iodice is surname
  and email and phone are blank</p>
  <p>basically it interprets it as if
  Dello is the surname and Iudice is the email.</p>
  <p>How do I tell him not to separate the surname if it composed by 2 or more words?</p>
  <p>PS: this a curiosity, but how do I Know if the csv file imported by the user is separated by ; and not , ? Because I set as separator &quot;;&quot;, but if the user imports a file that is separated by &quot;,&quot; the my code won't work, right?</p>