Django displays multitone field contents

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

It consists of the client, project, administrator and administrator classes, where admin_payment contains the identifiers of administrators and projects among others.

My question is, how can I display the "admin name" of each "project" in my project list? the project class itself does not contain administrator identifiers (Admin_Payment does this)

I currently 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 are displayed correctly in the project list, but admin names are not

Any help is appreciated (sorry if I posted anything that doesn't make sense, I'm new to python and django)

+8
django manytomanyfield
source share
1 answer

Displaying the contents of the ManyToMany field is not supported by django by default, as the database will be queried for each row of results. You can show it yourself by adding a method to 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!

+28
source share

Source: https://habr.com/ru/post/650555/


All Articles