unsupported operand type(s) for -: 'NoneType' and 'datetime.datetime'
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
add a comment |
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
Post the full Traceback error so the specific line can be isolated.
– berkelem
Nov 22 '18 at 15:10
add a comment |
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
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
python django
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
add a comment |
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
add a comment |
1 Answer
1
active
oldest
votes
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)
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Post the full Traceback error so the specific line can be isolated.
– berkelem
Nov 22 '18 at 15:10