Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using __str__() method in Django on Python 2

I am Learning Django using the Django project tutorial. Since I use python 2.7 I am unable to implement the following in python 2.7:

from django.db import models

class Question(models.Model):
# ...
    def __str__(self):              # __unicode__ on Python 2
        return self.question_text

class Choice(models.Model):
# ...
    def __str__(self):              # __unicode__ on Python 2
        return self.choice_text
like image 854
bhavya Avatar asked Feb 17 '15 18:02

bhavya


People also ask

What does the __STR__ method do in Django?

The __str__ method just tells Django what to print when it needs to print out an instance of the any model. It is also what lets your admin panel, go from this . You could choose what to show in the admin panel, as per your choice. Be it a field value or a default value or something else.

How to convert an object to a string in Django?

The method str allows us to convert an object into a string representation. This method is a dunder method which is usually used when creating models in Django, but it’s also used in other places. –> start and end with double underscores.

How do I use str() and Unicode() in Django?

Django provides a simple way to define str () and unicode () methods that work on Python 2 and 3: you must define a str () method returning text and to apply the python_2_unicode_compatible () decorator. ... This technique is the best match for Django’s porting philosophy.

What does the __STR__ function do in Python?

The __str__ function is used add a string representation of a model's object, so that is to tell Python what to do when we convert a model instance into a string. And if you dont mention it then it will take it by default the USERNAME_FIELD for that purpose.


2 Answers

To keep the code compatible between py2 and py3, a better way is to use the decorator python_2_unicode_compatible. This way you can keep the str method:

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

@python_2_unicode_compatible
class Question(models.Model):
# ...
    def __str__(self):              # __unicode__ on Python 2
        return self.question_text

@python_2_unicode_compatible
class Choice(models.Model):
# ...
    def __str__(self):              # __unicode__ on Python 2
        return self.choice_text

Reference: https://docs.djangoproject.com/en/1.8/topics/python3/#str-and-unicode-methods

Django provides a simple way to define str() and unicode() methods that work on Python 2 and 3: you must define a str() method returning text and to apply the python_2_unicode_compatible() decorator.

...

This technique is the best match for Django’s porting philosophy.

like image 111
alfetopito Avatar answered Nov 15 '22 20:11

alfetopito


Yes you can, you just replace __str__ with __unicode__, as the comment states:

class Question(models.Model):
# ...
    def __unicode__(self):
        return self.question_text

class Choice(models.Model):
# ...
    def __unicode__(self):
        return self.choice_text

Further down that section you'll find a bit of explanation:

__str__ or __unicode__?

On Python 3, it’s easy, just use __str__().

On Python 2, you should define __unicode__() methods returning unicode values instead. Django models have a default __str__() method that calls __unicode__() and converts the result to a UTF-8 bytestring. This means that unicode(p) will return a Unicode string, and str(p) will return a bytestring, with characters encoded as UTF-8. Python does the opposite: object has a __unicode__ method that calls __str__ and interprets the result as an ASCII bytestring. This difference can create confusion.

The question_text and choice_text attributes return Unicode values already.

like image 30
Martijn Pieters Avatar answered Nov 15 '22 19:11

Martijn Pieters