Last active
February 3, 2021 08:52
-
-
Save dmenisdev/95e40c62e08081556c828cd42d61777c to your computer and use it in GitHub Desktop.
Django admin - Show fields of related model in list view
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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