嘿,伙计们,我刚到Django,犯了一些幼稚的错误。
在我的模型中创建了一个项目名“singup”并添加了一个“smart_unicode实用程序”,但不幸的是,在我的管理站点中没有看到任何更改。我想将每个存储的注册都作为电子邮件调用。需要帮助,请
这是我的models.py文件:
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)
这是我的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)
这是我的管理网站图片。
最佳答案
方法的名称应为__unicode__
。有双下划线,不是单下划线。
def __unicode__(self):
return smart_unicode(self.email)
对于python 2
def __str__(self):
return smart_unicode(self.email)
所以完整的代码应该是
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)