django显示内容manytomanyfield

发布于 2024-09-14 04:34:53 字数 1181 浏览 3 评论 0原文

我试图在管理界面中显示多个字段的内容。我有以下代码:

class Member(models.Model):
    group = models.ManyToManyField('Group')

    def group_gp_name(self):
        return self.group.gp_name

    def __unicode__(self):

        return u'%s' % (self.id)

class Group(models.Model):
    gp_name = models.TextField(verbose_name=u'Group Name')

    def __unicode__(self):
        return u'%s - %s' % (self.id, self.gp_name)

在管理中我有类似这样的内容:

class MemberAdmin(admin.ModelAdmin):
    list_display = ('group_gp_name')

此方法适用于显示外键数据。显然这不适用于 ManytoManyFields.. 所以我的问题是,如何在管理页面的“成员”下显示我的组名称。因此,当我在管理员中单击“成员”时,我想立即查看由多对多关系耦合的组名称的内容?

更新!!! - 我不想在我的更改页面中显示它们,只是想查看表中的结果。我找到了这个,这几乎就是我想要的:

    def get_sol(self):
        return self.group.all()

这有效,但视图有点奇怪,它显示了这样的内容:

<Group: Administrators >]

问题是,我不想看到那些“[Group :”和“>]” ',那么我该如何摆脱这些呢?

更新2!!!

它帮助了我,但是如果发生这种情况怎么办?我有一个名为“测试”的第三个表,如下所示:

class Test(models.Model):
    member = models.ForeignKey('Member')

现在我想在管理视图“测试”中显示“组”表中的组名称,这怎么可能?

任何帮助表示赞赏。

问候, 时间

I'm trying to show the content of a manytomanyfield in the admin interface. I have the following code:

class Member(models.Model):
    group = models.ManyToManyField('Group')

    def group_gp_name(self):
        return self.group.gp_name

    def __unicode__(self):

        return u'%s' % (self.id)

class Group(models.Model):
    gp_name = models.TextField(verbose_name=u'Group Name')

    def __unicode__(self):
        return u'%s - %s' % (self.id, self.gp_name)

In the admin i have something like this:

class MemberAdmin(admin.ModelAdmin):
    list_display = ('group_gp_name')

This method worked for showing Foreignkey data. Obviously this doesn't work with ManytoManyFields.. so my question is, how can i show my group names in my admin page under Member. So when i click in the admin on 'Member' i want to see immediately the content of the Group names coupled by the manytomany relation?

UPDATE!!! - I don't want to show them in my change page is just want to see the result in the table. I've found this and it's almost what i want:

    def get_sol(self):
        return self.group.all()

This works but the view is little bit weird, it shows something like this:

<Group: Administrators >]

The problem is, i don't want to see those '[Group :' and '>]', so how do i get rid of these?

UPDATE2!!!

It helped me out, but what if for example this happens? I've got a 3rd table called Test like this:

class Test(models.Model):
    member = models.ForeignKey('Member')

Now i wanna show in the admin view 'Test' the group name from the table 'Group', how is that possible?

Any help is appreciated.

Regards,
T

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

溺ぐ爱和你が 2024-09-21 04:34:53

如果您想从更改页面查看多对多字段,那么您需要使用 管理内联

class GroupInline(admin.TabularInline):
    model = Group

class MemberAdmin(admin.ModelAdmin):
    list_display = ('group_gp_name')
    inlines = [
        GroupInline,
    ]

发布您的更新,如果您对列表显示而不是更改页面感兴趣,请尝试将 get_sol 更改为:

def get_sol(self):
  return '; '.join([group.gp_name for group in self.group.all()])

发布您的第二个更新,在 Test 上添加一个方法:

class Test(models.Model):
    member = models.ForeignKey('Member')

    def get_member_sol(self):
      return self.member.get_sol()

If you want to see your many to many field from the change page, then you need to use admin inlines.

class GroupInline(admin.TabularInline):
    model = Group

class MemberAdmin(admin.ModelAdmin):
    list_display = ('group_gp_name')
    inlines = [
        GroupInline,
    ]

Post your update, that you're interested in the list display rather than the change page, try changing get_sol to:

def get_sol(self):
  return '; '.join([group.gp_name for group in self.group.all()])

Post your second update, add a method on Test:

class Test(models.Model):
    member = models.ForeignKey('Member')

    def get_member_sol(self):
      return self.member.get_sol()
平生欢 2024-09-21 04:34:53

你不能那样使用它。因为ManyToManyFields创建了一个中间表来连接两个表。因此一组可能会返回多个结果。

def group_gp_name(self):
    a = self.group.select_related()
    # this will return you a list, so
    return ','.join(x.gp_name for x in a)

这将返回所有由 (,) 分隔的相关 pg_name 值

更新:

class Test(models.Model):
    member = models.ForeignKey('Member')


def get_gp_name(self):
    ','.join(x.gp_name for x in self.member.group.select_related())

由于 msmber 是外键,因此使用“.”不会有问题。使用外键关系的表示法。

You can not use it like that. Because ManyToManyFields creates an intermadiate table to connect two tables. So group may return more than one result.

def group_gp_name(self):
    a = self.group.select_related()
    # this will return you a list, so
    return ','.join(x.gp_name for x in a)

This will return you all related pg_name values separated by (,)

UPDATE :

class Test(models.Model):
    member = models.ForeignKey('Member')


def get_gp_name(self):
    ','.join(x.gp_name for x in self.member.group.select_related())

Since msmber is a foreignkey, you do not have problem using "." notation to use foreignkey relation.

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