0

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

7
  • which python version. Python 3 is unicode by default so why are u coercing the model into unicode? Commented Nov 23, 2017 at 11:31
  • i am using Python 2 Commented Nov 23, 2017 at 11:50
  • Really, why not use python3? Anyways.. put this at the top of your script # -*- coding: utf8 -*- Commented Nov 23, 2017 at 11:57
  • i have that at the top of each file Commented Nov 23, 2017 at 12:23
  • then you should not have the unicode method in your class again. Commented Nov 23, 2017 at 12:25

1 Answer 1

1

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__

Sign up to request clarification or add additional context in comments.

5 Comments

i have tried all...and i also tried this: def __unicode__(self): return '%mainid' % {'mainid': self.mainid} It still gives me that error
I did remove the def _str_ from both of them
If you are using the third, most explicit version, then it should be giving a different error, as that error wouldn't make sense.
k i got it...it doesn't give me anymore the error...its gives the column main from Main....when i refresh the page..it print's them out...but in the shell...it still gives me that error
And i just found out if you change something in models... you have reload the shell in order to take effect...just found out that now :))) Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.