2

I am using Python 2.7 and Django 1.9.7. I am trying to add a string method to models.py but I'm getting an error. When I run "python manage.py runserver", I get the following:

ValueError: @python_2_unicode_compatible cannot be applied to Languages because it doesn't define __ str__().

from django.db import models
from django.utils.encoding import python_2_unicode_compatible

@python_2_unicode_compatible
class Languages(models.Model):
    langid = models.AutoField(db_column='LangID', primary_key=True)
    lname = models.CharField(db_column='lName', max_length=50, blank=True, null=True)

    class Meta:
        db_table = 'languages'

    def __str__(self):
        return self.lname

This is very similar to the following Question: python_2_unicode_compatible error

But the problem there was that Django was not a new enough version. Clearly there is something different going on here. Any ideas on what the problem might be?

2
  • your snippet works fine on my computer. maybe try using 4 spaces instead of blank lines (say between lname and class Meta and before the definition of str) Commented Jun 15, 2016 at 19:14
  • @Ringil thanks, I checked but it doesn't seem to be an indentation problem. Commented Jun 15, 2016 at 19:46

2 Answers 2

2

When you are writing

    def __str__(self):
        return self.lname

Then you are over-riding the in-built 'str' method. So whenever str is used, your defined method is called instead of the in-built one.

So if you all you want to do is to return the "lname" of the object of the model "Languages", then you can use a different name of the def like this

    def lang_name(self):
        return u'%s' % self.lname

This u'%s' solves your problem of unicode to string.

Now, if you don't want to use other name of the def, but want to override the in-built 'str' function, then the below should work for you:

    def __str__(self):
        return u'%s' % self.lname

Though, by default Django models have the following function, which returns the default value for an object:

    def __unicode__(self):
        return u'%s' % self.lname
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this gives me an error though: "TypeError: 'class Meta' got invalid attribute(s): lang_name". Also the Django official tutorial documentation gave me the original code for def __ str__(self): return self.lname docs.djangoproject.com/en/1.9/intro/tutorial02
def lang_name(self): needs to be defined in the model and not under the Meta class as it is a property of the object of that model. Whereas in class Meta, you define representation (example -> ordering of objects, verbose_name etc) of the objects and not where you decide a new property for the objects See this -> stackoverflow.com/questions/26564001/… There are some pre-defined Meta options which you can use -> docs.djangoproject.com/en/dev/ref/models/options
1

I solved this by simply restarting the server.

Ctrl+C

and thereafter;

python manage.py runserver

Comments

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.