django display content of a manytomanyfield
Asked Answered
B

2

8

I started using django framework just a few days ago and i desperately need some help with my application.

It consists of client,project,admin,and admin payment classes where the admin_payment holds the ids of the admins and projects among other stuff.

My question is how i can display the "administrator's name" of each "project" in my admin listing of projects? the project class itself does not hold the administrator ids (the Admin_Payment does)

Currently i have the following structure: (striped down)

models.py

class Admin(models.Model):
    admin_name = models.CharField(unique = True, blank = False, null = False, max_length = 128, verbose_name = u'admin full name')

    def __unicode__(self):
        return self.admin_name
    class Meta:
        ordering = ('id',)
        verbose_name = u'Admin Info'

class Project(models.Model):
    client = models.ForeignKey(Client, verbose_name = u'Client')
    description = models.ForeignKey(Description, verbose_name = u'project description')
    admins = models.ManyToManyField(Admin, verbose_name = u'Administrators', through = 'Admin_Payment')

class Admin_Payment(models.Model):
    admin = models.ForeignKey(Admin, verbose_name = u'Administrator')
    project = models.ForeignKey(Project, verbose_name = u'project')

admin.py (striped down)

class AdminInline(admin.TabularInline):
    model = Admin

class ProjectAdmin(admin.ModelAdmin):
    radio_fields = {'position': admin.HORIZONTAL, 'solution': admin.HORIZONTAL}
    inlines = [AdminInline, ]
    list_display = ['client','description','admin_name']

Clients and Descriptions appear correctly in the project listing but the admin names are not

Any help is appreciated (sorry if i posted anything that doesnt make sense , i am a newbie in python and django)

Bowdlerize answered 30/12, 2010 at 15:58 Comment(0)
I
30

Displaying the contents of ManyToMany field isn't supported by default by django, because the database will be queried for each row of the results. You can display it yourself by adding a method to your Project-model:

class Project(models.Model):
    ....
    def admin_names(self):
        return ', '.join([a.admin_name for a in self.admins.all()])
    admin_names.short_description = "Admin Names"

and put admin_names in your list_display fields!

Idioplasm answered 30/12, 2010 at 16:44 Comment(5)
That worked great thanks. Any idea how i can add a filter for each admin?Bowdlerize
is it possible to add a verbose_name to the returned value?Bowdlerize
@Thordin9: i've added the answer to fit yourquestion, additionally see the doc at docs.djangoproject.com/en/dev/ref/contrib/admin/…Idioplasm
Useful answer. If you want to add the value from the __unicode__ method on your model without rewriting it, try: return ', '.join([str(a) for a in self.admins.all()]). It works fine if you don't use the __str__ method as stated on documentation.Metamorphose
Is there a way to show this collection in nested list?Fictive
S
0

You can get the queryset of a m2m field by using all() twice. So for a project instance of Project you can get the admins by:

proj1 = Project.objects ...

proj1.admins.all().all()

which will result in the queryset of the admins on proj1.

Shirelyshirey answered 12/6 at 7:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.