You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Django Admin后台自定义多对多关联Inline的显示文本

Customizing the Many-to-Many Relationship Display in Django Admin

Absolutely! The generic "Project-technology relationship: #1" text comes from Django's auto-generated through table for your many-to-many field. To replace this with something more meaningful (like combining the project and technology names), you just need to explicitly define the through model and override its __str__ method. Here's how to implement this:

Step 1: Update Your Models to Explicitly Define the Through Model

In your models.py, create a dedicated through model for the Project-Technology relationship, then link it to your ManyToManyField:

class Technology(models.Model):
    title = models.CharField(max_length=10)
    class Meta:
        verbose_name_plural = 'Technologies'
    def __str__(self):
        return self.title

# Explicit through model for the many-to-many relationship
class ProjectTechnology(models.Model):
    project = models.ForeignKey(Project, on_delete=models.CASCADE)
    technology = models.ForeignKey(Technology, on_delete=models.CASCADE)

    # Customize the display string here
    def __str__(self):
        # Example: Show project title + technology name
        return f"{self.project.title} uses {self.technology.title}"

class Project(models.Model):
    title = models.CharField(max_length=100)
    description = HTMLField()
    # Link the many-to-many field to our explicit through model
    technology = models.ManyToManyField(
        Technology, 
        related_name='projects', 
        through=ProjectTechnology
    )
    image = models.ImageField(upload_to='projects/')
    def __str__(self):
        return self.title

Step 2: Update the Admin Inline to Use the New Through Model

In your admin.py, modify the inline class to reference your new ProjectTechnology model instead of the auto-generated through table:

class TechnologyInline(admin.StackedInline):
    # Use our explicit through model instead of Project.technology.through
    model = ProjectTechnology

@admin.register(Technology)
class TechnologyAdmin(admin.ModelAdmin):
    pass

@admin.register(Project)
class ProjectAdmin(admin.ModelAdmin):
    inlines = [
        TechnologyInline,
    ]
    exclude = [
        'technology',
    ]

Step 3: Run Database Migrations

Since we've explicitly defined the through model (even though it matches Django's auto-generated structure under the hood), you'll need to run migrations to update Django's schema records:

python manage.py makemigrations
python manage.py migrate

Once you do this, the admin interface will display your custom string (like "My Portfolio Site uses Python") instead of the generic relationship text. You can tweak the __str__ method in ProjectTechnology to any format that fits your needs—for example, return f"[{self.project.title}] - {self.technology.title}" or whatever makes the relationship clear at a glance.

内容的提问来源于stack exchange,提问作者Omid Shojaee

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.29 20:32:39