Put data from an Excel spreadsheet in the database

0

The database here in the company generates an Excel report, I need to save this data in a database, initially sqlite3, to be able to display this data in a Django template in the future.

Example of exel:

Nome do profissional,"Situação atual","Endereco E_Mail","Site","Site 2","Nascimento",
ABDIAS VENCESLAU DA SILVA NETO,"Ativo","[email protected]","","","07/07/1977",

Each such data would be stored in the respective Django model and in the database.

What would be the best way to do this? Send suggestions please!

    
asked by anonymous 27.02.2018 / 17:04

1 answer

0

Try this out

models.py

class Funcionario(models.Model):
    nome = models.CharField(max_length=255),
    situacao = models.BooleanField,
    email = models.CharField(max_length=255)
    site = models.CharField(max_length=255)
    site2 = models.CharField(max_length=255)
    nascimento = models.DateField()

services.py (or any other file)

class ImportToDatabase(object):

    @staticmethod
    def import_data():
        with open('relatorio.csv') as csvfile:
            reader = csv.reader(csvfile, delimiter=',')
            for row in reader:
                obj, created = Funcionario.objects.get_or_create(
                    nome=row[0],
                    situacao=row[1],
                    email=row[2],
                    site=row[3],
                    site2=row[4],
                    nascimento=row[5]
                )

views.py

ImportToDatabase.import_data()

If you want to use the Employee object it is loaded in the variable obj To see if the record has been inserted or loaded, use the boolean variable "created".

    
27.02.2018 / 19:30