【问题标题】:Django: how to change manytomany inline text with no intermediate table?Django:如何在没有中间表的情况下更改多行内联文本?
【发布时间】:2015-12-05 21:58:38
【问题描述】:

当您拥有我所说的“中间”ManyToMany 表时,就很容易了,就像这样:

class Interrogation(models.Model):
    phrases = models.ManyToManyField(Phrase, blank=True,
                                     through='InterrogationPhrase',
                                     symmetrical=False,
                                     related_name='phrase')

class InterrogationPhrase(models.Model):
    interrogation = models.ForeignKey(Interrogation, blank=False)
    phrase = models.ForeignKey(Phrase, blank=False)

    def __str__(self):
        return u'({0}) - {1} - {2}'.format(
            self.importance, self.interrogation, self.phrase
        )

使用上面的代码,在“Inline”管理中,如果我这样声明,它会显示InterrogationPhrase__str__

class InterrogationPhrasesInline(CollapsedStackedInline):
    model = InterrogationPhrase
    fk_name = 'interrogation'
    fields = ('phrase')
    extra = 0
    verbose_name = _(u"Phrase")
    verbose_name_plural = _(u"Phrases")

但是当我不想中间声明而只想写的时候怎么处理:

class Mot(models.Model):
    groupes = models.ManyToManyField(Groupe)

没有声明MotGroupe 表(这在Django 中是允许的)?我的管理员是这样的:

class MotGroupesInline(CollapsedStackedInline):
    model = Mot.groupes.through
    extra = 0
    verbose_name = _(u"Groupe")
    verbose_name_plural = _(u"Groupes")

class MotAdmin(admin.ModelAdmin):
    inlines = (MotGroupesInline,)

但问题在于,对于Groupe 的每一行,内联显示为:Groupe: Mot_groupes object。这既丑陋又无用。怎么改?

【问题讨论】:

    标签: django django-models django-admin


    【解决方案1】:

    您可以使用Proxy models

    我的models.py

    # coding: utf-8
    from __future__ import unicode_literals
    from django.db import models
    
    
    class Groupe(models.Model):
        name = models.CharField(max_length=255)
    
        def __str__(self):
            return self.name
    
    
    class Mot(models.Model):
        name = models.CharField(max_length=255)
        groupes = models.ManyToManyField(Groupe)
    
        def __str__(self):
            return self.name
    
    
    class MotGroupeProxy(Mot.groupes.through):
        class Meta:
            proxy = True
    
        def __str__(self):
            return str(self.groupe)
    

    我的admin.py

    # coding: utf-8
    from django.contrib import admin
    from django.utils.translation import ugettext_lazy as _
    
    from so_34111398_manytomany_inline.models import Mot, Groupe, MotGroupeProxy
    
    
    class MotGroupesInline(admin.StackedInline):
        model = MotGroupeProxy
        extra = 0 
        verbose_name = _(u"Groupe")
        verbose_name_plural = _(u"Groupes")
    
    
    @admin.register(Mot)
    class MotAdmin(admin.ModelAdmin):
        inlines = (MotGroupesInline,)
    
    
    @admin.register(Groupe)
    class GroupeAdmin(admin.ModelAdmin):
        pass
    

    结果是http://screencloud.net/v/eN4K

    【讨论】:

    • 我检查您的解决方案是否有效,因为这就是我所做的,但我想要更简单的东西。我认为有一种方法可以避免 MotGroupeProxy... Django 非常强大,通常有很多快捷方式。
    • 你用什么 Django 插件来改变管理界面?
    • 我花了很多时间挖掘 Django 内部结构以查看其他选项。对我来说,代理是最好的,但您可以肯定地创建一个特殊的管理模板github.com/django/django/blob/master/django/contrib/admin/…。这是 Django 1.9 中的新管理 UI
    猜你喜欢
    • 2011-01-27
    • 2019-10-09
    • 2021-12-02
    • 2020-12-24
    • 2013-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-25
    相关资源
    最近更新 更多