views:

39

answers:

2

Hi all,

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

A: 

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()
Dominic Rodger
Could you look at my update2 please? Your solution worked aswell
Thomas
@Thomas - done.
Dominic Rodger
Thank you very much, that works great! I'm getting to understand the behaviour. Anyway when i want to get the values out of the manytomany field can i just use something like this in the view?test = Test.objects.get(id=1)solutuion = test.member.group.gp_nameThis gets me the solution related to that object into a variable without any '[ Group' and '>]' brackets or is there another method for that?
Thomas
@Thomas - To get the group names for a member you need to do something like `'; '.join([group.gp_name for group in test.member.group.all()])` or just `test.get_member_sol()`. If my answer is helpful, it's polite to upvote it (use the arrows next to the answer), and mark it as accepted (use the checkmark next to the answer).
Dominic Rodger
But i mean inside my views.py function? I have to use the same method to show all related solution names or can i just do like i said in my previous comment?test = Test.objects.get(id=1) solutuion = test.member.group.gp_nameI will upvote it, just wanna know what to do in the views.py
Thomas
since you can not use self in a view function, you must get an instance of your object with test = Test.objects.get(id=1). Then you can use test.member.get_sol()
FallenAngel
I advise you to have a look at http://docs.djangoproject.com/en/1.2/ Django have a good documantation. Every question you asked has an answer in there. http://www.djangobook.com/ alsa have good examples.
FallenAngel
A: 

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.

FallenAngel
to write a vomment, i divide function in two seperate lines, of course you can use return ','.join(x.gp_name for x in self.group.select_related()) instead
FallenAngel
Could you look at my UPDATE2 please, your solution worked great!
Thomas