Eduardo Luz
Eduardo Luz

Reputation: 71

Django ManyToManyField in admin value list

class Schedule(models.Model):
    landing = models.ManyToManyField(Place, related_name="landing_schedule", null=False)    

    def __unicode__(self):
        return u'%s | %s' % (self.departure, self.landing)

How can i display the values inside admin for the ManyToManyField ?

Upvotes: 1

Views: 100

Answers (2)

Amar
Amar

Reputation: 666

You can write a method as below and make it display in admin list display model.py

class Schedule(models.Model):
    landing =models.ManyToManyField(Place,related_name="landing_schedule", null=False)
    #
    # 
    def list_of_landing(self):
        return ",".join([x.something for x in self.place.all()])

admin.py

class ScheduleAdmin(admin.ModelAdmin):

    list_display = ('list_of_landing')

Upvotes: 0

Mike Covington
Mike Covington

Reputation: 2167

Assuming Place has a name field, the following should do what you want.

def __unicode__(self):
    return u'%s | %s' % (self.departure, ", ".join(l.name for l in self.landing.all()))

Depending on how many records you have and how many relationships, it might cause a performance hit, so be careful.

Upvotes: 1

Related Questions