开发者

django display content manytomanyfield

开发者 https://www.devze.com 2023-01-11 01:03 出处:网络
I\'m trying to show the content of a manytomanyfield in the admin interface. I have the following code:

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 helpe开发者_JAVA百科d 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


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()


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消