python – Django Admin – 显示M2M模型的中介字段

python – Django Admin – 显示M2M模型的中介字段,第1张

概述我们有一个Django应用程序,其中包含一份报纸文章列表.每篇文章都与“发言人”以及“公司”(文章中提到的公司)有m2m关系. 目前,用于创建新文章的添加文章页面非常接近我们想要的 – 它只是股票Django Admin,我们使用filter_horizo​​ntal来设置两个m2m关系. 下一步是在每个m2m关系上添加“评级”字段作为中间字段. 所以,我们的models.py的一个例子 clas 我们有一个Django应用程序,其中包含一份报纸文章列表.每篇文章都与“发言人”以及“公司”(文章中提到的公司)有m2m关系.

目前,用于创建新文章的添加文章页面非常接近我们想要的 – 它只是股票Django admin,我们使用filter_horizo​​ntal来设置两个m2m关系.

下一步是在每个m2m关系上添加“评级”字段作为中间字段.

所以,我们的models.py的一个例子

class Article(models.Model):    Title = models.CharFIEld(max_length=100)    publication_date = models.DateFIEld()    entry_date = models.DateFIEld(auto_Now_add=True)    abstract = models.TextFIEld() # Can we restrict this to 450 characters?    category = models.ForeignKey(category)    subject = models.ForeignKey(Subject)    weekly_summary = models.BooleanFIEld(help_text = 'Should this article be included in the weekly summary?')    source_publication = models.ForeignKey(Publication)    page_number = models.CharFIEld(max_length=30)    article_softcopy = models.fileFIEld(upload_to='article_scans',null=True,blank=True,help_text='Optionally upload a soft-copy (scan) of the article.')    url = models.URLFIEld(null=True,help_text = 'Enter a URL for the article. Include the protocl (e.g. http)')    firm = models.ManyToManyFIEld(Firm,through='Firmrating')    spokesperson = models.ManyToManyFIEld(Spokeperson,through='Spokespersonrating')    def __unicode__(self):        return self.Titleclass Firm(models.Model):    name = models.CharFIEld(max_length=50,unique=True)    homepage = models.URLFIEld(verify_exists=False,help_text='Enter the homepage of the firm. Include the protocol (e.g. http)')    def __unicode__(self):        return self.name    class Meta:        ordering = ['name']class Spokeperson(models.Model):    Title = models.CharFIEld(max_length=100)    first_name = models.CharFIEld(max_length=50)    last_name = models.CharFIEld(max_length=50)    def __unicode__(self):        return self.first_name + ' ' + self.last_name    class Meta:        ordering = ['last_name','first_name']class Firmrating(models.Model):    firm = models.ForeignKey(Firm)    article = models.ForeignKey(Article)    rating = models.IntegerFIEld()class Spokespersonrating(models.Model):    firm = models.ForeignKey(Spokesperson)    article = models.ForeignKey(Article)    rating = models.IntegerFIEld()

这里的问题是,一旦我们将我们的公司和发言人字段更改为“通过”并使用中介,我们的添加文章页面就不再具有filter_horizo​​ntal控件来向文章添加Firms / Spokeperson关系 – 它们完全消失.你根本看不到它们.我不知道为什么会这样.

我希望有一些方法继续使用酷filter_horizo​​ntal小部件来设置关系,并以某种方式只是嵌入下面的另一个字段来设置评级.但是,我不知道如何做到这一点,同时仍然利用Django管理员.

我在这里看到一篇关于覆盖Django管理员中的单个小部件的文章:

http://www.fictitiousnonsense.com/archives/22

但是,我不确定该方法是否仍然有效,而且我不确定是否将它应用于此处,将FK应用于中间模型(它基本上是内联的?).

当然有一种简单的方法可以做到这一切吗?

干杯,
胜利者

解决方法 问题是django.contrib.admin.options中的admin方法formfIEld_for_manytomany没有返回带有中间模型的多个字段的表单字段! http://code.djangoproject.com/browser/django/trunk/django/contrib/admin/options.py#L157

您必须在Modeladmin中覆盖此方法:

def formfIEld_for_manytomany(self,db_fIEld,request=None,**kwargs):    """    Get a form FIEld for a ManyToManyFIEld.    """    # If it uses an intermediary model that isn't auto created,don't show    # a fIEld in admin.    if not db_fIEld.rel.through._Meta.auto_created:        return None    # return something suitable for your needs here!    db = kwargs.get('using')    if db_fIEld.name in self.raw_ID_fIElds:        kwargs['Widget'] = Widgets.ManyToManyRawIDWidget(db_fIEld.rel,using=db)        kwargs['help_text'] = ''    elif db_fIEld.name in (List(self.filter_vertical) + List(self.filter_horizontal)):        kwargs['Widget'] = Widgets.FilteredSelectMultiple(db_fIEld.verbose_name,(db_fIEld.name in self.filter_vertical))
总结

以上是内存溢出为你收集整理的python – Django Admin – 显示M2M模型的中介字段全部内容,希望文章能够帮你解决python – Django Admin – 显示M2M模型的中介字段所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1192391.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-03
下一篇 2022-06-03

发表评论

登录后才能评论

评论列表(0条)

保存