Python 2.7__unicode __ (self) not working

unicode (self) does not work for me. I still see the "Name of the object" in the admin. My code is as follows:

import datetime # standard python datetime module from django.db import models # Djangos time-zone-related utilities from django.utils import timezone class Name(models.Model): name = models.CharField(max_length=200) def __unicode__(self): # Python 3: def __str__(self): return self.name 

Thank you

+4
source share
2 answers

The problem is that you need to define the __unicode__ method in the class definition.

 import datetime # standard python datetime module from django.db import models # Djangos time-zone-related utilities from django.utils import timezone class Name(models.Model): name = models.CharField(max_length=200) def __unicode__(self): # Python 3: def __str__(self): return str(self.name) 

should work for you.

+10
source

Python INDENTION will respond most of the time, work with correlation, use an editor, or select _unicode_ (self) with a tab

  def __unicode__(self): # Python 3: def __str__(self): return str(self.name) 
+1
source

Source: https://habr.com/ru/post/1479476/


All Articles