-1
I’m trying to enter records into my database, specifically the model Participante
from a file .csv
, whose format is id, name, Cpf and email.
So far what I have in mine views.py
is:
def presenca(request):
form = UploadArquivo()
if request.method == 'POST':
formulario = UploadArquivo(request.POST, request.FILES)
#data_evento = '-'.join(formulario.data['data'].split('/')[::-1])
if formulario.is_valid():
formulario.save(commit=False)
formulario.save()
arquivo = str(request.FILES.getlist('upload_csv')[0]) # pega o nome do arquivo
f = open('csv/' + arquivo.replace(' ', '_'), 'r')
for line in f:
line = line.split(',') # separado por vírgula
participante = Participante.objects.filter(cpf=line[2]) # pega o CPF da planilha
if participante: # se o cpf está preenchido
verifica = Participante.objects.filter(cpf=participante) # verifica se o cpf da planilha coincide com algum do banco
if not verifica: # se nao coincide
cadastra = Participante.objects.create(participante__nome=line[0], participante__cpf=line[1], participante__email=line[2])
presenca = Participante(cadastra).save() # salva os dados do participante no model Participante
f.close()
return render(request, 'base.html', locals())
But you’re not saving it in the bank. If someone can give me a light, I would appreciate it. Thank you from now on for the attention!!
How is the model? More precisely
Participante
.– Augusto Vasques
class Participante(models.Model):
 nome = models.CharField(max_length=100)
 cpf = models.CharField(max_length=13)
 email = models.EmailField()
 dt_criacao = models.DateTimeField(auto_now=True)

– BeatrizGomes