尝试在Django管理界面显示Many to Many关系的反向关系

huangapple go评论48阅读模式
英文:

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 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"

英文:

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(&#39;Musician&#39;, blank=True)

class Musician(models.Model):
    id = models.IntegerField().primary_key = True
    name = models.CharField(max_length=2083, blank=True, null=True)
    &lt;NAME OF BAND&gt;

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 = [&quot;member_of&quot;] in my MusicianAdmin

答案1

得分: 0

如果您指定了ManyToManyField(或任何其他关系),则可以使用以下方式自动进行反向查询:

my_musician.band_set.all()

这是一个包含与名为musicianMusician相关联的所有BandQuerySet

您可以将这些添加到音乐家的ModelAdmin中:

class BandMembersInline(admin.TabularInline):
    model = Band.members.through

@admin.register(Musician)
class MusicianAdmin(admin.ModelAdmin):
    inlines = [BandMembersInline,]

但是每个Musician只能属于一个Band

如果每个音乐家只能拥有一个乐队,则建模是错误的。在这种情况下,您可以使用从MusicianBandForeignKey来建模:

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&nbsp;<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):
# &hellip;
pass

@admin.register(Band)
class BandAdmin(admin.ModelAdmin):
# &hellip;
inlines = [<b>MusicianInline</b>]</code></pre>

huangapple
  • 本文由 发表于 2023年6月26日 05:28:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定