classic: unsupported operand type(s) for +: 'int' and 'NoneType'

20 views
Skip to first unread message

makayabou

unread,
Apr 13, 2015, 8:05:49 PM4/13/15
to django...@googlegroups.com
Hello,
I'm sorry for that trivial problem, but I'm still a bit lost...

[code]
#In models.py:

class Ordi(models.Model):
RAM_CAP_1 = 1024
RAM_CAP_2 = 2048
RAM_CAP_CHOICES = (
(RAM_CAP_1, u'1G'),
(RAM_CAP_2, u'2G'),
)
ram_1 = models.IntegerField("memory slot 1", max_length=10, choices=RAM_CAP_CHOICES, null= True, blank=True)
ram_2 = models.IntegerField("memory slot 2", max_length=10, choices=RAM_CAP_CHOICES, null= True, blank=True)

def ram_total(self):
ram_tot = self.ram_capacite_1 + self.ram_capacite_2
#
# Here, python can't contenate int and NoneType...
#
return ram_tot
def maintenus(self):
ordis_maintenus = self.maintenance_set.all()
for o in ordis_maintenus:
return o.__unicode__
    def __unicode__(self):
        return 'ordi '+unicode(self.id)+' - '+self.renvoios()


class Maintenance(models.Model):
    ordi = models.ForeignKey(Ordi)
    def __unicode__(self):
        return self.titre + " du " +self.date_sortie.strftime('%m/%d/%Y')
        
[/code]

makayabou

unread,
Apr 13, 2015, 8:07:27 PM4/13/15
to django...@googlegroups.com
It's  ram_tot = self.ram_1 + self.ram_2

that returns me "cannot contenate int and NOne

Bill Blanchard

unread,
Apr 13, 2015, 8:10:43 PM4/13/15
to django...@googlegroups.com
If you allow Null values in your model and one of the two is being populated with Null, then you'll get the error you're seeing.  Example:

Form:
ram_1 = 56
ram_2 = Null (None)

56 + Null (None) doesn't work.

What you should do instead is set Null to FALSE in both your field declarations and set a default value (it could be zero if you want, but since you have two choices, you can make it one of the two as well).  You could leave it as null=True and set a default value, but that seems like bad practice.

Bill

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/f30ef94f-55b4-482f-9b39-a007e99321ff%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages