Skip to content

Instantly share code, notes, and snippets.

@dmenisdev
Last active February 3, 2021 08:52
Show Gist options
  • Save dmenisdev/95e40c62e08081556c828cd42d61777c to your computer and use it in GitHub Desktop.
Save dmenisdev/95e40c62e08081556c828cd42d61777c to your computer and use it in GitHub Desktop.
Django admin - Show fields of related model in list view
Class AlbumAdmin(ImportExportModelAdmin, admin.ModelAdmin):
list_display=('name', 'release_date', 'artist', 'get_musician_instrument')
list_filter=('name')
def get_musician_instrument(self,obj):
Return obj.artist.instrument
get_musician_instrument.admin_order_field='artist' # allowscolumnordersorting
get_musician_instrument.short_description='Instrument' # overwritecolumnname
#Registeryourmodelshere
admin.site.register(Album,AlbumAdmin)
from django.db import models
class Musician(models.Model):
first_name = models.CharField(max_length=50)
last_name = models.CharField(max_length=50)
instrument = models.CharField(max_length=100)
class Album(models.Model):
artist = models.ForeignKey(Musician, on_delete=models.CASCADE)
name = models.CharField(max_length=100)
release_date = models.DateField()
num_stars = models.IntegerField()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment