I'm surprised that your BooleanFields
have the empty string as their value. Regardless, since booleans evaluate to 0
and 1
in a numeric context, you can just do:
self.total = bool(self.uno) + bool(self.dos)
Question
How can I interpret booleans (or '') as integers 0 or 1? so totals could be 0, 1 or 2, depending on the values of uno and dos.
class foo(models.Model)
uno = models.BooleanField()
dos = models.BooleanField()
total = models.PositiveSmallIntegerField(blank=True, default=int(0))
def save(self, *args, **kwargs):
# HUMDINGER....
self.total = int(self.uno) + int(self.dos)
super(Survey, self).save(*args, **kwargs) # Call the "real" save() method.
This is the error it is throwing for that line...
invalid literal for int() with base 10: ''
Solution
I'm surprised that your BooleanFields
have the empty string as their value. Regardless, since booleans evaluate to 0
and 1
in a numeric context, you can just do:
self.total = bool(self.uno) + bool(self.dos)