Reputation: 267
i m getting this error:
>>> Child.objects.all()
Traceback (most recent call last):
File "<input>", line 1, in <module>
Child.objects.all()
u = six.text_type(self)
TypeError: coercing to Unicode: need string or buffer, Main found
Whenever i try to pull an object from Child. i tried to use the unicode but it still gives me this error. I also commented the **def str ** thought that was a problem. I don't know what i can do about it? Please help
class Main(models.Model):
website = models.CharField(db_column='Website', unique=True, max_length=250)
main = models.CharField(db_column='Main', unique=True, max_length=100)
tablename = models.CharField(db_column='TableName', unique=True, max_length=100)
created_at = models.DateTimeField(db_column='Created_at')
class Meta:
managed = False
db_table = 'Main'
# def __str__(self):
# return self.main
def __unicode__(self):
return unicode(self.main)
class Child(models.Model):
mainid = models.ForeignKey(Main, models.DO_NOTHING, db_column='MainID')
day = models.IntegerField(db_column='Day')
hour = models.TimeField(db_column='HOUR')
created_at = models.DateTimeField(db_column='Created_at')
class Meta:
managed = False
db_table = 'Child'
# def __str__(self):
# return self.mainid
def __unicode__(self):
return unicode(self.mainid)
Thank you
Upvotes: 0
Views: 58
Reputation: 11931
The problem is that you are passing a main object to the unicode function. It expects a string of a buffer, but you are giving it an object.
Try changing your __unicode_ method in the Child. (There are a few ways that will work)
class Child(models.Model):
...
...
..
def __unicode__(self):
return self.mainid.__unicode__()
or this should work too by implicity caling the unicode method on the Main object.
def __unicode__(self):
return self.mainid
or this
def __unicode__(self):
return self.mainid.main
Not, that the __str__ method is what Python 3 uses whereas Python 2 uses __unicode__
Upvotes: 1