Django, Displaying and editing reverse foreign-key relationships in admin
Asked Answered
T

1

6

I am making a web game in which each world can be part of one alliance.

class World(models.Model):
    # rest of Model
    alliance = models.ForeignKey('Alliance', related_name='allmember', default=None, blank=True, null=True)
    officer = models.NullBooleanField()
    leader = models.NullBooleanField()

class Alliance(models.Model):
    allianceid = models.AutoField(primary_key=True)
    alliance_name = models.CharField(max_length=20, unique=True)
    alliance_desc = models.TextField(max_length=200)

I gather that using inlines, I can display the members of an alliance on the alliance page. However, I can only edit the officer and leader statuses, whereas I want to be able to edit the membership status as well. Here is the inline I am using.

class MemberInline(admin.TabularInline):
    model = World
    fk_name = 'alliance'
    # excludes

class AllianceAdmin(admin.ModelAdmin):
    inlines = [
        MemberInline,
    ]

I suppose what I really want to ask is if I can edit a foreign key relationship in the admin site from the target model rather than the originator.

Tong answered 15/10, 2013 at 12:2 Comment(5)
What membership status?Wrasse
I'd like to be able to edit whether a world is in an alliance from the alliance page.Tong
But you can do that already - there's automatically a dropdown for alliance on the World admin form.Wrasse
True, but like I said, I want to able to edit this from the Alliance admin form. After all, if I'm managing members of an alliance it makes sense for me to be able to do this from the alliance form rather than having to go to each individual member form.Tong
@Tong maybe this is helpful https://mcmap.net/q/644723/-django-admin-select-reverse-foreign-key-relationships-not-create-i-want-to-add-availableInverness
P
-1

It´s very old post but is good for reference. This should work! Did you pass the AllianceAdmin to the register function?

admin.site.register(Alliance, AllianceAdmin)

Putto answered 28/2, 2019 at 23:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.