英文:
Trying to display the reverse of a Many to Many Relationship in Django Admin
问题
"Say I'm working on a database that tracks the members in different bands. Each Band will have multiple Musicians, but each Musician can only be in one Band. If my models look like this:
class Band(models.Model):
    id = models.IntegerField().primary_key = True
    name = models.CharField(max_length=2083, blank=True, null=True)
    members = models.ManyToManyField('Musician', blank=True)
class Musician(models.Model):
    id = models.IntegerField().primary_key = True
    name = models.CharField(max_length=2083, blank=True, null=True)
    <NAME OF BAND>
After adding the Musicians in my db to the Bands, when I go to edit a Musician on the Admin page I want to see which Band they are in. What do I need to add to my models or my ModelAdmins to make that happen?
Right now where the above say 
member_of: Band.name
I would expect that to return the name of the band but it's giving me the name of the Musician
Quick Edit: I forgot to mention that I have readonly_fields = ["member_of"] in my MusicianAdmin"
英文:
Say I'm working on a database that tracks the members in different bands. Each Band will have multiple Musicians, but each Musician can only be in one Band. If my models look like this:
class Band(models.Model):
    id = models.IntegerField().primary_key = True
    name = models.CharField(max_length=2083, blank=True, null=True)
    members = models.ManyToManyField('Musician', blank=True)
class Musician(models.Model):
    id = models.IntegerField().primary_key = True
    name = models.CharField(max_length=2083, blank=True, null=True)
    <NAME OF BAND>
After adding the Musicians in my db to the Bands, when I go to edit a Musician on the Admin page I want to see which Band they are in. What do I need to add to my models or my ModelAdmins to make that happen?
Right now where the above say <NAME OF BAND> I have:
member_of: Band.name
I would expect that to return the name of the band but it's giving me the name of the Musician
Quick Edit: I forgot to mention that I have readonly_fields = ["member_of"] in my MusicianAdmin
答案1
得分: 0
如果您指定了ManyToManyField(或任何其他关系),则可以使用以下方式自动进行反向查询:
my_musician.band_set.all()
这是一个包含与名为musician的Musician相关联的所有Band的QuerySet。
您可以将这些添加到音乐家的ModelAdmin中:
class BandMembersInline(admin.TabularInline):
    model = Band.members.through
@admin.register(Musician)
class MusicianAdmin(admin.ModelAdmin):
    inlines = [BandMembersInline,]
但是每个Musician只能属于一个Band。
如果每个音乐家只能拥有一个乐队,则建模是错误的。在这种情况下,您可以使用从Musician到Band的ForeignKey来建模:
class Band(models.Model):
    name = models.CharField(max_length=2083)
    def __str__(self):
        return self.name
class Musician(models.Model):
    name = models.CharField(max_length=2083)
    band = models.ForeignKey(
        Band, related_name='members', on_delete=models.CASCADE
    )
作为管理员,您可以使用以下方式:
class MusicianInline(admin.TabularInline):
    model = Musician
@admin.register(Musician)
class MusicianAdmin(admin.ModelAdmin):
    # ...
    pass
@admin.register(Band)
class BandAdmin(admin.ModelAdmin):
    # ...
    inlines = [MusicianInline]
这些是您提供的代码段的翻译部分。
英文:
If you specify a ManyToManyField (or any other relation), you can query in reverse automatically with:
<pre><code>my_musician<b>.band_set.all()</b></code></pre>
This is a QuerySet of all Bands related to the Musician named musician.
You can add these to your ModelAdmin for the musicians with:
<pre><code>class BandMembersInline(admin.TabularInline):
model = <b>Band.members.through</b>
@admin.register(Musician)
class MusicianAdmin(admin.ModelAdmin):
inlines = [
<b>BandMembersInline</b>,
]</code></pre>
> but each Musician can only be in one Band.
If each musician can only have one band, the modeling is wrong. In that case you model this with a ForeignKey <sup>[Django-doc]</sup> from Musician to Band:
<pre><code>class Band(models.Model):
name = models.CharField(max_length=2083)
def __str__(self):
    return self.name
class Musician(models.Model):
name = models.CharField(max_length=2083)
<b>band = models.ForeignKey(</b>
Band, <b>related_name='members'</b>, on_delete=models.CASCADE
<b>)</b></code></pre>
as admin, you can then use:
<pre><code>class MusicianInline(admin.TabularInline):
model = Musician
@admin.register(Musician)
class MusicianAdmin(admin.ModelAdmin):
# …
pass
@admin.register(Band)
class BandAdmin(admin.ModelAdmin):
# …
inlines = [<b>MusicianInline</b>]</code></pre>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论