Django 无法以更改形式显示 model.id?

发布于 2024-11-01 06:41:27 字数 1217 浏览 1 评论 0原文

我有一个模型帖子,我想在模型收藏夹的管理中显示其数据。

帖子模型:

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)
  1. 如果我这样使用代码,当我尝试通过其管理员添加收藏夹时,我会收到以下错误:

    <块引用>

    渲染时捕获类型错误:强制转换为 Unicode:需要字符串或缓冲区,找到 int

  2. 好的,所以它不喜欢显示整数,所以我想我只显示昵称,因为它是 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)
  1. 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

  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧伤慢歌 2024-11-08 06:41:27
  1. 我通常这样做:

    类 Post(models.Model):
        昵称 = models.CharField(max_length=50, default="anonymous")
    
        def __unicode__(自身):
            返回 u"%s" % self.id
    
  2. 字段(docs) 是如何控制管理“添加”和“更改”页面的布局。对于您来说,能够在不只是用于显示的字段列表中包含方法是没有意义的。因此 list_display 允许您包含方法。

  1. I usually do this:

    class Post(models.Model):
        nickname = models.CharField(max_length=50, default="anonymous")
    
        def __unicode__(self):
            return u"%s" % self.id
    
  2. 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.

木森分化 2024-11-08 06:41:27

Python 需要一个 unicode 字符串,但在这两种情况下你都给它提供了其他东西。

在这两种情况下,简单地转换为 unicode 字符串应该可以解决问题。

return unicode(self.id)

return unicode(self.post.nickname)

编辑:您实际上不需要投射该部分,抱歉。只需将 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.

return unicode(self.id)

and

return unicode(self.post.nickname)

edit: You don't actually need to cast that part, sorry. Just the int cast to unicode should solve both problems.

长途伴 2024-11-08 06:41:27

您已更改 post.__unicode__() 例程以返回 int!

使用默认值不是问题.

关于list_display需要注意的一些特殊情况:

如果字段是ForeignKey,Django将显示__unicode__()
相关对象。

You've altered the post.__unicode__() routine to return an int!

This is not a problem using the defaults.

A few special cases to note about list_display:

If the field is a ForeignKey, Django will display the __unicode__() of
the related object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文