django显示内容manytomanyfield
我试图在管理界面中显示多个字段的内容。我有以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想从更改页面查看多对多字段,那么您需要使用 管理内联。
发布您的更新,如果您对列表显示而不是更改页面感兴趣,请尝试将
get_sol
更改为:发布您的第二个更新,在
Test
上添加一个方法:If you want to see your many to many field from the change page, then you need to use admin inlines.
Post your update, that you're interested in the list display rather than the change page, try changing
get_sol
to:Post your second update, add a method on
Test
:你不能那样使用它。因为ManyToManyFields创建了一个中间表来连接两个表。因此一组可能会返回多个结果。
这将返回所有由 (,) 分隔的相关 pg_name 值
更新:
由于 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.
This will return you all related pg_name values separated by (,)
UPDATE :
Since msmber is a foreignkey, you do not have problem using "." notation to use foreignkey relation.