Black User
Black User

Reputation: 405

smart_unicode not working in my Django Project

I am new to Django and Stuck in some childish error. I made a project name "singup" and added a "smart_unicode utility" in my models but unfortunately,in my admin site i am not seeing any changes. I want to call each stored signup as an email.

Here is my models.py file:

from django.db import models
from django.utils.encoding import smart_unicode
# Create your models here.
class signup(models.Model):
    first_name=models.CharField(max_length=12,null=False,blank=False)
    last_name=models.CharField(max_length=12,null=False,blank=False)
    email=models.EmailField(max_length=60,null=False,blank=False)
    timestamp=models.DateTimeField(auto_now_add=True,auto_now=False)
    updated=models.DateTimeField(auto_now_add=False,auto_now=True)


def __unicode__(self):
    return smart_unicode(self.email)

Here is My admin.py

    from django.contrib import admin

# Register your models here.
from models import signup

class signupAdmin(admin.ModelAdmin):
    class Meta:
        model=signup
admin.site.register(signup,signupAdmin)

And here is my Admin site image.

enter image description here

Upvotes: 3

Views: 4214

Answers (3)

Saad Gauhar
Saad Gauhar

Reputation: 21

This is the solution for python above 2.7

from django.utils.encoding import smart_text

...

def __str__(self):
        return smart_text(self.email)

Upvotes: 2

jax
jax

Reputation: 4197

Same problem i was facing then i realize that the indentation of my last block was not correct.

Just look at the code you have pasted here its showing the same, just give a tab or 4 spaces to "def unicode(self):" and dont change anything as it will work I am sure: it worked form me

It should be under class then it will work fine:

from django.db import models
from django.utils.encoding import smart_unicode
# Create your models here.
class signup(models.Model):
    first_name=models.CharField(max_length=12,null=False,blank=False)
    last_name=models.CharField(max_length=12,null=False,blank=False)
    email=models.EmailField(max_length=60,null=False,blank=False)
    timestamp=models.DateTimeField(auto_now_add=True,auto_now=False)
    updated=models.DateTimeField(auto_now_add=False,auto_now=True)

    def __unicode__(self):
        return smart_unicode(self.email)

Because this method def "unicode" belongs to class signup, and when you put "def unicode" at zero indentation block it looses its relation to signup class and becomes and independent method, and hence calling this signup class from admin section return class as an object. But when you put "def unicode" method under the roof of class signup at first indentation level. its belongs to signup and return unicode, chars as emails.

Upvotes: 0

utkbansal
utkbansal

Reputation: 2817

the method's name should be __unicode__. There are double underscores, not single.

def __unicode__(self):
    return smart_unicode(self.email)

for python 2

def __str__(self):
        return smart_unicode(self.email)

So the complete code should be

class signup(models.Model):
    first_name=models.CharField(max_length=12,null=False,blank=False)
    last_name=models.CharField(max_length=12,null=False,blank=False)
    email=models.EmailField(max_length=60,null=False,blank=False)
    timestamp=models.DateTimeField(auto_now_add=True,auto_now=False)
    updated=models.DateTimeField(auto_now_add=False,auto_now=True)


    def __str__(self):
        return smart_unicode(self.email)

Upvotes: 1

Related Questions