Unicode in django 2

def __unicode__(self) in django
django unicode to string
python_2_unicode_compatible django 3
django smart_text
django models
python unicode
from __future__ import unicode_literals
django python 3

I am used to using print to print the django model object that I define in __unicode__, but it seems that no longer works:

def __unicode__(self):
    """GMT-03:00 Palmer"""
    return "GMT-%s %s" % (self.offset_time, self.name)



>>> t=Timezone.objects.all()[0]
>>> print (t)
Timezone object (Andorra)
 >>> t.__unicode__()
'GMT-02:00 Andorra'

How would I call the "print" method, like it was previously done in django, to return the unicode method?

What's happening here is that print() needs a string representation of the object. This happens by first looking for a __str__() method and falling back on the __repr__() method if that doesn't exist.

If there's no __str__() method, the final fallback is object.__repr__() at the end of the inheritance chain. Or in the case of django model objects, django.db.models.Model.__str__() which gives you the output seen in the question.

In django < version 2.0, the __unicode__ method was used instead of __str__ in template rendering. The reason was python 2 compatibility. In current versions of django, use __str__() instead.

Python 2.x's Unicode implementation really leaves something to be desired. The major issue often arises when you need to go between str()  However, in an international environment, you might need to construct a URL from an IRI – very loosely speaking, a URI that can contain Unicode characters. Use these functions for quoting and converting an IRI to a URI: The django.utils.encoding.iri_to_uri() function, which implements the conversion from IRI to URI as required by RFC 3987#section-3.1.

According to Django's documentation, you should define your model's __str__() method to get a string representation of it. Although, in older versions of Django and using python 2, you used to do it by defining __unicode__().

All of Django's database backends automatically convert Unicode strings into the appropriate Python 2 with unicode literals or Python 3:. Unicode data¶ Django supports Unicode data everywhere. This document tells you what you need to know if you’re writing applications that use data or templates that are encoded in something other than ASCII.

__unicode__ should be implemented when you are using Python 2 in order to have a string representation of your model.

If you are using Python 3, you should implement __str__ instead. From the docs, implementing this method allows you to have a human-readable representation of the model.

Python 2. Python 3. class Person(models.Model): name = models.TextField() def __unicode__(self): return self.name, class Person(models.Model): name  Django natively supports Unicode data everywhere. Providing your database can somehow store the data, you can safely pass around Unicode strings to templates, models and the database. This document tells you what you need to know if you’re writing applications that use data or templates that are encoded in something other than ASCII.

This HOWTO discusses Python 2.x's support for Unicode, and explains various problems that people commonly encounter when trying to work with Unicode. Django natively supports Unicode data everywhere. Providing your database can somehow store the data, you can safely pass around strings to templates, models, and the database. This document tells you what you need to know if you’re writing applications that use data or templates that are encoded in something other than ASCII.

for details on how to set or alter the database character set encoding. * PostgreSQL users, refer to the `PostgreSQL manual`_ (section 21.2.2 in. PostgreSQL 8) for  try. from django.utils.six import python_2_unicode_compatible instead of. from django.utils.encoding import python_2_unicode_compatible and let me know if this works.

On Python 2, adding a unicode_literals import to mypaths.py would change the return type of the unix_style_path function from str to unicode in the user code,  OP missed the fact that Django 2.X does not even support Python 2 (both unicode_literals and __unicode__ are from the Python 2 days) – DeepSpace Aug 26 '19 at 21:14

Comments
  • You can use print(unicode(t)). Note however that in Python-3.x, unicode(..) has been removed, since str now supports unicode strings.
  • @WillemVanOnsem got it -- thanks for your continued help on these django questions!
  • @DavidL, what version of Python and Django are you using?