unsupported operand type(s) for -: 'NoneType' and 'datetime.datetime'












0















is giving this error, but before it was not, I do not know what happened



Model.py



class MovRotativo(models.Model):
checkin = models.DateTimeField(auto_now=False, null=False, blank=False)
checkout = models.DateTimeField(auto_now=False, null=True, blank=True)
valor_hora = models.DecimalField(max_digits=5, decimal_places=2)
veiculo = models.ForeignKey(Veiculo, on_delete=models.CASCADE)
pago = models.BooleanField(default=False)

def horas_total(self):
return math.ceil((self.checkout - self.checkin).total_seconds() / 3600)

def total(self):
return self.valor_hora * self.horas_total()

def __str__(self):
return self.veiculo.placa


views.py



@login_required
def movrotativos_novo(request):
form = MovRotativoForm(request.POST or None)
if form.is_valid():
form.save()
return redirect('core_lista_movrotativos')









share|improve this question

























  • Post the full Traceback error so the specific line can be isolated.

    – berkelem
    Nov 22 '18 at 15:10
















0















is giving this error, but before it was not, I do not know what happened



Model.py



class MovRotativo(models.Model):
checkin = models.DateTimeField(auto_now=False, null=False, blank=False)
checkout = models.DateTimeField(auto_now=False, null=True, blank=True)
valor_hora = models.DecimalField(max_digits=5, decimal_places=2)
veiculo = models.ForeignKey(Veiculo, on_delete=models.CASCADE)
pago = models.BooleanField(default=False)

def horas_total(self):
return math.ceil((self.checkout - self.checkin).total_seconds() / 3600)

def total(self):
return self.valor_hora * self.horas_total()

def __str__(self):
return self.veiculo.placa


views.py



@login_required
def movrotativos_novo(request):
form = MovRotativoForm(request.POST or None)
if form.is_valid():
form.save()
return redirect('core_lista_movrotativos')









share|improve this question

























  • Post the full Traceback error so the specific line can be isolated.

    – berkelem
    Nov 22 '18 at 15:10














0












0








0








is giving this error, but before it was not, I do not know what happened



Model.py



class MovRotativo(models.Model):
checkin = models.DateTimeField(auto_now=False, null=False, blank=False)
checkout = models.DateTimeField(auto_now=False, null=True, blank=True)
valor_hora = models.DecimalField(max_digits=5, decimal_places=2)
veiculo = models.ForeignKey(Veiculo, on_delete=models.CASCADE)
pago = models.BooleanField(default=False)

def horas_total(self):
return math.ceil((self.checkout - self.checkin).total_seconds() / 3600)

def total(self):
return self.valor_hora * self.horas_total()

def __str__(self):
return self.veiculo.placa


views.py



@login_required
def movrotativos_novo(request):
form = MovRotativoForm(request.POST or None)
if form.is_valid():
form.save()
return redirect('core_lista_movrotativos')









share|improve this question
















is giving this error, but before it was not, I do not know what happened



Model.py



class MovRotativo(models.Model):
checkin = models.DateTimeField(auto_now=False, null=False, blank=False)
checkout = models.DateTimeField(auto_now=False, null=True, blank=True)
valor_hora = models.DecimalField(max_digits=5, decimal_places=2)
veiculo = models.ForeignKey(Veiculo, on_delete=models.CASCADE)
pago = models.BooleanField(default=False)

def horas_total(self):
return math.ceil((self.checkout - self.checkin).total_seconds() / 3600)

def total(self):
return self.valor_hora * self.horas_total()

def __str__(self):
return self.veiculo.placa


views.py



@login_required
def movrotativos_novo(request):
form = MovRotativoForm(request.POST or None)
if form.is_valid():
form.save()
return redirect('core_lista_movrotativos')






python django






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 15:11









Risadinha

9,30115562




9,30115562










asked Nov 22 '18 at 15:03









Mauricio KalfelzMauricio Kalfelz

288




288













  • Post the full Traceback error so the specific line can be isolated.

    – berkelem
    Nov 22 '18 at 15:10



















  • Post the full Traceback error so the specific line can be isolated.

    – berkelem
    Nov 22 '18 at 15:10

















Post the full Traceback error so the specific line can be isolated.

– berkelem
Nov 22 '18 at 15:10





Post the full Traceback error so the specific line can be isolated.

– berkelem
Nov 22 '18 at 15:10












1 Answer
1






active

oldest

votes


















1














Note that the checkout field allows null values. I'm guessing the error is raised on the horas_total method for records with null in the checkout field.
The reason this error wasn't raised previously is probably that there weren't any records missing the checkout field.



Edit: As for a solution, you can enforce the checkout field to have a value. Alternatively you can check for None:



def horas_total(self):
if self.checkout is None:
# handle case where there's no value for checkout
return math.ceil((self.checkout - self.checkin).total_seconds() / 3600)





share|improve this answer


























  • You could add the specific code to illustrate where to check for None.

    – Risadinha
    Nov 22 '18 at 15:13











  • def horas_total(self): if self.checkout is None: pass else: return math.ceil((self.checkout - self.checkin).total_seconds() / 3600).

    – Mauricio Kalfelz
    Nov 23 '18 at 15:34













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433695%2funsupported-operand-types-for-nonetype-and-datetime-datetime%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














Note that the checkout field allows null values. I'm guessing the error is raised on the horas_total method for records with null in the checkout field.
The reason this error wasn't raised previously is probably that there weren't any records missing the checkout field.



Edit: As for a solution, you can enforce the checkout field to have a value. Alternatively you can check for None:



def horas_total(self):
if self.checkout is None:
# handle case where there's no value for checkout
return math.ceil((self.checkout - self.checkin).total_seconds() / 3600)





share|improve this answer


























  • You could add the specific code to illustrate where to check for None.

    – Risadinha
    Nov 22 '18 at 15:13











  • def horas_total(self): if self.checkout is None: pass else: return math.ceil((self.checkout - self.checkin).total_seconds() / 3600).

    – Mauricio Kalfelz
    Nov 23 '18 at 15:34


















1














Note that the checkout field allows null values. I'm guessing the error is raised on the horas_total method for records with null in the checkout field.
The reason this error wasn't raised previously is probably that there weren't any records missing the checkout field.



Edit: As for a solution, you can enforce the checkout field to have a value. Alternatively you can check for None:



def horas_total(self):
if self.checkout is None:
# handle case where there's no value for checkout
return math.ceil((self.checkout - self.checkin).total_seconds() / 3600)





share|improve this answer


























  • You could add the specific code to illustrate where to check for None.

    – Risadinha
    Nov 22 '18 at 15:13











  • def horas_total(self): if self.checkout is None: pass else: return math.ceil((self.checkout - self.checkin).total_seconds() / 3600).

    – Mauricio Kalfelz
    Nov 23 '18 at 15:34
















1












1








1







Note that the checkout field allows null values. I'm guessing the error is raised on the horas_total method for records with null in the checkout field.
The reason this error wasn't raised previously is probably that there weren't any records missing the checkout field.



Edit: As for a solution, you can enforce the checkout field to have a value. Alternatively you can check for None:



def horas_total(self):
if self.checkout is None:
# handle case where there's no value for checkout
return math.ceil((self.checkout - self.checkin).total_seconds() / 3600)





share|improve this answer















Note that the checkout field allows null values. I'm guessing the error is raised on the horas_total method for records with null in the checkout field.
The reason this error wasn't raised previously is probably that there weren't any records missing the checkout field.



Edit: As for a solution, you can enforce the checkout field to have a value. Alternatively you can check for None:



def horas_total(self):
if self.checkout is None:
# handle case where there's no value for checkout
return math.ceil((self.checkout - self.checkin).total_seconds() / 3600)






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 15:16

























answered Nov 22 '18 at 15:05









andersourceandersource

51418




51418













  • You could add the specific code to illustrate where to check for None.

    – Risadinha
    Nov 22 '18 at 15:13











  • def horas_total(self): if self.checkout is None: pass else: return math.ceil((self.checkout - self.checkin).total_seconds() / 3600).

    – Mauricio Kalfelz
    Nov 23 '18 at 15:34





















  • You could add the specific code to illustrate where to check for None.

    – Risadinha
    Nov 22 '18 at 15:13











  • def horas_total(self): if self.checkout is None: pass else: return math.ceil((self.checkout - self.checkin).total_seconds() / 3600).

    – Mauricio Kalfelz
    Nov 23 '18 at 15:34



















You could add the specific code to illustrate where to check for None.

– Risadinha
Nov 22 '18 at 15:13





You could add the specific code to illustrate where to check for None.

– Risadinha
Nov 22 '18 at 15:13













def horas_total(self): if self.checkout is None: pass else: return math.ceil((self.checkout - self.checkin).total_seconds() / 3600).

– Mauricio Kalfelz
Nov 23 '18 at 15:34







def horas_total(self): if self.checkout is None: pass else: return math.ceil((self.checkout - self.checkin).total_seconds() / 3600).

– Mauricio Kalfelz
Nov 23 '18 at 15:34






















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53433695%2funsupported-operand-types-for-nonetype-and-datetime-datetime%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

If I really need a card on my start hand, how many mulligans make sense? [duplicate]

Alcedinidae

Can an atomic nucleus contain both particles and antiparticles? [duplicate]