I'm trying to break some rock by developing a website on my own, and I'm starting by creating some registry pages and listing database records.
我试图通过自己开发一个网站来打破一些摇滚,我开始创建一些注册表页面并列出数据库记录。
I'm getting bugged with the fact that __unicode__
method doesn't print the username of my records and __str__
does!
我收到了__unicode__方法不打印我的记录的用户名和__str__的事实!
I know that using __unicode__
is the best practice to have, but I can only print my object username with __str__
.
我知道使用__unicode__是最好的做法,但我只能用__str__打印我的对象用户名。
Can anybody explain why this happens?
任何人都能解释为什么会这样吗?
My Model:
我的型号:
from django.db import models
class User(models.Model):
username = models.CharField(max_length=200)
reg_date = models.DateTimeField('registry date')
def __unicode__(self):
return self.username
My admin.py:
我的admin.py:
from django.contrib import admin
from registo.models import User
admin.site.register(User)
My __unicode__(self)
output:
我的__unicode __(自我)输出:
User
User object
My __str__(self)
output:
我的__str __(自我)输出:
User
Teste
Thanks for your cooperation in advance!
感谢您的提前合作!
2 个解决方案
#1
13
it looks like you are using Python3.x
and here is the relevant documentation on Str and Unicode methods
看起来你正在使用Python3.x,这里是关于Str和Unicode方法的相关文档
In Python 2, the object model specifies
__str__()
and__unicode__()
methods. If these methods exist, they must return str (bytes) and unicode (text) respectively.在Python 2中,对象模型指定__str __()和__unicode __()方法。如果存在这些方法,则它们必须分别返回str(字节)和unicode(文本)。
The print statement and the str() built-in call
__str__()
to determine the human-readable representation of an object. The unicode() built-in calls__unicode__()
if it exists, and otherwise falls back to__str__()
and decodes the result with the system encoding. Conversely, the Model base class automatically derives__str__()
from__unicode__()
by encoding to UTF-8.print语句和str()内置调用__str __()来确定对象的人类可读表示。 unicode()内置调用__unicode __()(如果存在),否则返回__str __()并使用系统编码对结果进行解码。相反,Model基类通过编码为UTF-8自动从__unicode __()派生__str __()。
In Python 3, there’s simply
__str__()
, which must return str (text).在Python 3中,只有__str __(),它必须返回str(文本)。
So
所以
On Python 3, the decorator is a no-op. On Python 2, it defines appropriate
__unicode__()
and__str__()
methods (replacing the original__str__()
method in the process).在Python 3上,装饰器是一个无操作器。在Python 2上,它定义了适当的__unicode __()和__str __()方法(替换了进程中的原始__str __()方法)。
#2
2
If it's not the python 3 thing, your code as posted has incorrect indentation - not sure if copy/pasting bug or if that's how it is in the code. But your User
model's methods need to be indented, like so:
如果它不是python 3的东西,你发布的代码有不正确的缩进 - 不确定是否复制/粘贴错误或者它是如何在代码中。但是你的用户模型的方法需要缩进,如下所示:
from django.db import models
class User(models.Model):
username = models.CharField(max_length=200)
reg_date = models.DateTimeField('registry date')
def __unicode__(self):
return self.username
#1
13
it looks like you are using Python3.x
and here is the relevant documentation on Str and Unicode methods
看起来你正在使用Python3.x,这里是关于Str和Unicode方法的相关文档
In Python 2, the object model specifies
__str__()
and__unicode__()
methods. If these methods exist, they must return str (bytes) and unicode (text) respectively.在Python 2中,对象模型指定__str __()和__unicode __()方法。如果存在这些方法,则它们必须分别返回str(字节)和unicode(文本)。
The print statement and the str() built-in call
__str__()
to determine the human-readable representation of an object. The unicode() built-in calls__unicode__()
if it exists, and otherwise falls back to__str__()
and decodes the result with the system encoding. Conversely, the Model base class automatically derives__str__()
from__unicode__()
by encoding to UTF-8.print语句和str()内置调用__str __()来确定对象的人类可读表示。 unicode()内置调用__unicode __()(如果存在),否则返回__str __()并使用系统编码对结果进行解码。相反,Model基类通过编码为UTF-8自动从__unicode __()派生__str __()。
In Python 3, there’s simply
__str__()
, which must return str (text).在Python 3中,只有__str __(),它必须返回str(文本)。
So
所以
On Python 3, the decorator is a no-op. On Python 2, it defines appropriate
__unicode__()
and__str__()
methods (replacing the original__str__()
method in the process).在Python 3上,装饰器是一个无操作器。在Python 2上,它定义了适当的__unicode __()和__str __()方法(替换了进程中的原始__str __()方法)。
#2
2
If it's not the python 3 thing, your code as posted has incorrect indentation - not sure if copy/pasting bug or if that's how it is in the code. But your User
model's methods need to be indented, like so:
如果它不是python 3的东西,你发布的代码有不正确的缩进 - 不确定是否复制/粘贴错误或者它是如何在代码中。但是你的用户模型的方法需要缩进,如下所示:
from django.db import models
class User(models.Model):
username = models.CharField(max_length=200)
reg_date = models.DateTimeField('registry date')
def __unicode__(self):
return self.username