Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python 2.7__unicode__(self) not working

Tags:

python

django

unicode(self) is not working for me. I can still see 'Name 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

Thanking you

like image 365
Imran Omar Bukhsh Avatar asked Dec 20 '22 06:12

Imran Omar Bukhsh


1 Answers

The problem you have is that you need to define the __unicode__ method within 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.

like image 177
Snakes and Coffee Avatar answered Jan 02 '23 19:01

Snakes and Coffee