I have 2 apps: Visitors
and Meetings
, linked via a ManytoMany
field:
visitors/models.py:
from django.db import models
from meetings.models import Meeting
class Visitor(models.Model):
visitor_name = models.CharField(default='name', max_length=128, blank=False, null=False)
visitor_meetings = models.ManyToManyField(Meeting)
def __str__(self):
return self.visitor_name
meetings/models.py:
from django.db import models
from team.models import Team
class Meeting(models.Model):
team_member = models.ForeignKey(Team)
meeting_name = models.CharField(default='name', max_length=128, blank=True, null=True)
def __str__(self):
return self.meeting_name
I know the correct way to get a list_display
for meetings in the Visitors
admin panel is here: https://stackoverflow.com/a/18108586/2429989.
However, how can I show a list_display
for visitors at each meeting in the Meetings
admin panel? I have tried:
meetings/admin.py:
from django.contrib import admin
from .models import Meeting
from visitors.models import Visitor
class MeetingAdmin(admin.ModelAdmin):
list_display = ['id', 'team_member', 'show_visitors' ]
def show_visitors(self, obj):
return "\n".join([a.visitor_name for a in obj.visitor.all()])
admin.site.register(Meeting, MeetingAdmin)
This results in 'Meeting' object has no attribute 'visitor'
, where am I going wrong?
The default reverse lookup relation it's always obj.tablename_set
. In your case you need to use obj.visitor_set.all()
class MeetingAdmin(admin.ModelAdmin):
list_display = ['id', 'team_member', 'show_visitors' ]
def show_visitors(self, obj):
return "\n".join([a.visitor_name for a in obj.visitor_set.all()])
However, you can customize the name of the reverse lookup by defining the related_name
:
class Visitor(models.Model):
visitor_name = models.CharField(default='name', max_length=128, blank=False, null=False)
visitor_meetings = models.ManyToManyField(Meeting, related_name='visitors')
def __str__(self):
return self.visitor_name
All Meeting
objects will now have access to visitors
. Use it like:
class MeetingAdmin(admin.ModelAdmin):
list_display = ['id', 'team_member', 'show_visitors' ]
def show_visitors(self, obj):
return "\n".join([a.visitor_name for a in obj.visitors.all()])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With