django: coercing to Unicode: need string or buffer, int found
Solution 1
Apparently, item.quantity
is an integer, and pi.quantity
is unicode.
I'm guessing pi.quantity
was assigned a string previously (which django will convert upon db save), but it won't return the coerced value until you instantiate the class again. The value is cached according to some shell sessions.
Just do pi.quantity = int(pi.quantity) + item.quantity
or look to where pi.quantity
was defined and use integers instead!
Solution 2
Your code : def unicode(self): return self.id
Should be: def unicode(self): return unicode(self.id )
Comments
-
abolotnov almost 2 years
I'm getting this error when trying to sum up two int values:
if dups.count() > 0: for item in dups: pi.quantity = pi.quantity+item.quantity
both pi and dups are instance of same model:
class PurchaseItem(models.Model): picture = models.ForeignKey(Picture, null=False) paperType = models.ForeignKey(paperType, null=False) printSize = models.ForeignKey(printSize, null=False) quantity = models.IntegerField(default=1, validators=[validators.MinValueValidator(1)]) price = models.DecimalField(decimal_places=2,max_digits=8) dateCreated = models.DateTimeField(null=False) sessionKey = models.ForeignKey(Session, to_field="session_key", null=False) user = models.ForeignKey(User,null=True) def __unicode__(self): return self.id
Why is int not good enough?
if I wrap the values with str() or use their .str() representation, it doesn't do quite what I need. 1 and 1 will before 11 instead of 2.