Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In the Django Admin Site, how can I access model properties through an Inline?

Tags:

python

django

models.py:

class Player(models.Model):
    name = models.CharField(max_length=50)
    email = models.EmailField(max_length=50)

class Tournament(models.Model):
    name = models.CharField(max_length=50)

class TournamentPlayer(models.Model):
    tournament = models.ForeignKey(Tournament)
    player = models.ForeignKey(Player)
    paid = models.BooleanField()

    def player_email(self):
        return self.player.email

admin.py:

class TournamentPlayerInline(admin.TabularInline):
    model = TournamentPlayer
    fields = ('player', 'paid', 'player_email')

@admin.register(Tournament)
class TournamentAdmin(admin.ModelAdmin):
    inlines = [TournamentPlayerInline]

I have an Inline question. When I pull up a Tournament in the Admin Site, I can see which players are going, and if they paid. I would also like to display extra information contained in Player, for example email address.

In TournamentPlayerInline I thought I might be able to get away with fields = ('player', 'paid', 'player_email') but I get FieldError: Unknown field(s) (player_email) specified for TournamentPlayer.

I also tried fields = ('player', 'paid', 'player__email'), but I get FieldError: Unknown field(s) (player__email) specified for TournamentPlayer.

If I move player_email from fields to readonly_fields, I no longer get the error, but the player email also isn't displayed either.

This is what I'm after:

enter image description here

How can I access Player properties from TournamentPlayerInline?

Django 1.8.4

like image 557
epalm Avatar asked Oct 06 '15 14:10

epalm


People also ask

How do I access my Django admin page?

To login to the site, open the /admin URL (e.g. http://127.0.0.1:8000/admin ) and enter your new superuser userid and password credentials (you'll be redirected to the login page, and then back to the /admin URL after you've entered your details).

What is the correct way to include Django's admin urls?

You need to include the name of the url pattern as well, e.g. {% url "admin:index" %} for the admin index.


2 Answers

Monkey's answer is almost correct. The only change you have to make is to your admin.py, and it's merely adding 'player_email' to both fields as well as readonly_fields. Changing the position of 'player_email' in fields will allow you to order it as per your example.

class TournamentPlayerInline(admin.TabularInline):
    model = TournamentPlayer
    fields = ('player', 'player_email', 'paid',)
    readonly_fields = ('player_email',)

@admin.register(Tournament)
class TournamentAdmin(admin.ModelAdmin):
    inlines = [TournamentPlayerInline]
like image 162
Anthony Clever Avatar answered Oct 21 '22 15:10

Anthony Clever


If you do not require the player_email to be editable from the inline, then you can accomplish this with the readonly_fields variable:

class TournamentPlayerInline(admin.TabularInline):
    model = TournamentPlayer
    fields = ('player', 'paid')
    readonly_fields = ('player_email',)

@admin.register(Tournament)
class TournamentAdmin(admin.ModelAdmin):
    inlines = [TournamentPlayerInline]
like image 24
Monkpit Avatar answered Oct 21 '22 15:10

Monkpit