Save Timefield as blank

Asked

Viewed 39 times

0

I have a function to save the data imported from excel, the problem is that I have a Timefield that can come blank, and with that I have the following message:

Error saving object to database: ('expected string or bytes-like Object',)

#Função Importar Dados do Excel:
    def inventario_import(request):
    usuario = request.user.id
    print('Inicio de Importação Usuário: ', usuario)
    if request.method == 'POST':
        new_inventarios = request.FILES['myfile']
        df = pd.read_excel(new_inventarios)
        df.head()
        # print('Arquivo: ', df)

        today = date.today()
        # n = 0
        for index, row in df.iterrows():

            iip = ''
            field = ''


            col1 = row['SIGLA']
            col2 = str(row['LOJA'])
            col3 = row['DATA']
            col4 = row['TIPO']
            col5 = row['SETOR']
            col6 = row['PESSOAS']
            col7 = row['REGIONAL']
            col8 = row['LIDER']
            col9 = row['INICIO']

# Atribuindo hora de inicio
                    field8 = col9
#Função para Salvar
    inv = Inventario(loja=lj, dataInclusao=today, usuarioInclusao=us, dataInventario=field5,
                                         tipo=tp, horaIni=field8, setor=st,
                                         pessoasReal=field3, regional=rg)
    inv.save()

I tried to assign None that way on field8 to resolve but unsuccessfully:

if field8:
   pass
else:
field8 = None

Someone has been through this problem and knows how to solve it ?

Thank you.

#Campo do Model do Field HORA

    horaIni = models.TimeField(auto_now=False, auto_now_add=False, blank=True, null=True)
  • Rodolfo, you forgot to post the most important code, the code of your Model. How did you define your Timefield? And your code field8 = col9 Loose ain’t good for much...

  • @fernandosavio I’m sorry, I updated the question, please see if you can understand.

1 answer

0


After studying a little I managed to solve this problem using the Try argument. I try to assign the value, if it fails, it saves the others without problems.

invf = Inventario(loja=lj, dataInclusao=today, usuarioInclusao=us, dataInventario=field5,
                                      tipo=tp, pessoasReal=field3, regional=rg, id_pai=iip, filhoInventario=True)
                    invf.save()

                    try:
                        invf.horaIni = field8
                        invf.save()
                    except:
                        pass

                    invf.lider.add(ld)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.