Django 无法以更改形式显示 model.id?
我有一个模型帖子,我想在模型收藏夹的管理中显示其数据。
帖子模型:
class Post(models.Model):
nickname = models.CharField(max_length=50, default="anonymous")
def __unicode__(self):
return self.id
最喜欢的模型:
class Favorite(models.Model):
user = models.ForeignKey(User, unique=False)
post = models.ForeignKey(Post, unique=False)
def __unicode__(self):
return self.user.username
def get_post_nickname(self):
return self.post.nickname
最喜欢的管理员:
class FavoriteAdmin(admin.ModelAdmin):
#fields = ('user', 'get_post_nickname')
list_display= ('user', 'post')
def save_model(self, request, obj, form, change):
obj.save()
admin.site.register(Favorite, FavoriteAdmin)
如果我这样使用代码,当我尝试通过其管理员添加收藏夹时,我会收到以下错误:
<块引用>渲染时捕获类型错误:强制转换为 Unicode:需要字符串或缓冲区,找到 int
好的,所以它不喜欢显示整数,所以我想我只显示昵称,因为它是 Charfield。我取消注释了FavoriteAdmin() 中调用FavoriteModel.get_post_nickname() 的第一行。现在我收到以下错误:
“FavoriteAdmin.fields”引用表单中缺少的字段“get_post”。
如何同时解决#1 和#2?难道不能调用 fields() 中的方法吗?我认为这是因为 list_display() 接受一个方法。
I have a model Post whose data I would like to display in model Favorite's Admin.
Post model:
class Post(models.Model):
nickname = models.CharField(max_length=50, default="anonymous")
def __unicode__(self):
return self.id
Favorite model:
class Favorite(models.Model):
user = models.ForeignKey(User, unique=False)
post = models.ForeignKey(Post, unique=False)
def __unicode__(self):
return self.user.username
def get_post_nickname(self):
return self.post.nickname
Favorite Admin:
class FavoriteAdmin(admin.ModelAdmin):
#fields = ('user', 'get_post_nickname')
list_display= ('user', 'post')
def save_model(self, request, obj, form, change):
obj.save()
admin.site.register(Favorite, FavoriteAdmin)
If I use the code as such, when I try to add a Favorite through its Admin I get the following error:
Caught TypeError while rendering: coercing to Unicode: need string or buffer, int found
OK so it doesn't like to display an Integer, so I thought I'll just display the nickname since its a Charfield. I uncommented the first line in FavoriteAdmin() to call FavoriteModel.get_post_nickname(). Now I get the following error:
'FavoriteAdmin.fields' refers to field 'get_post' that is missing from the form.
How can I solve both #1 and #2? Is it not possible to call a method in fields()? I thought it would be since list_display() accepts a method.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我通常这样做:
字段(
docs
) 是如何控制管理“添加”和“更改”页面的布局。对于您来说,能够在不只是用于显示的字段列表中包含方法是没有意义的。因此 list_display 允许您包含方法。I usually do this:
Fields (
docs
) is how to control the layout of admin "add" and "change" pages. It wouldn't make sense for you to be able to include methods in a list of fields that aren't for just display. Hence list_display allows you to include methods.Python 需要一个 unicode 字符串,但在这两种情况下你都给它提供了其他东西。
在这两种情况下,简单地转换为 unicode 字符串应该可以解决问题。
和编辑:您实际上不需要投射该部分,抱歉。只需将 int 转换为 unicode 就可以解决这两个问题。
Python is expecting a unicode string, but in both instances you're feeding it something else.
In both instance a simple cast to a unicode string should fix the issue.
andedit: You don't actually need to cast that part, sorry. Just the int cast to unicode should solve both problems.
您已更改 post.
__unicode__
() 例程以返回 int!使用默认值不是问题.
You've altered the post.
__unicode__
() routine to return an int!This is not a problem using the defaults.