管理员在反向相关字段上的django过滤器 [英] django filter in admin on reverse related field

查看:55
本文介绍了管理员在反向相关字段上的django过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模型:

class Supplier(models.Model):
    unique_id = models.IntegerField(unique=True)
    name = models.CharField(max_length=255, unique=True)
    address = models.CharField(max_length=255, blank=True, null=True)
    email = models.EmailField(blank=True, null=True)
    telephone = models.CharField(max_length=15, blank=True, null=True)

class PaymentMethod(models.Model):
    unique_id = models.CharField(max_length=3)
    supplier = models.ForeignKey(Supplier, null=True)
    last_updated = models.DateTimeField(auto_now=True)

为此,我尝试为供应商添加用于付款方式的过滤器:

For which I trying to add a filter to suppliers for payment method:

@admin.register(Supplier)
class SupplierAdmin(admin.ModelAdmin):
    list_display = ('unique_id', 'name', 'last_updated')
    ordering = ('unique_id',)
    list_filter = ('payment_method__unique_id')
    inlines = [PaymentMethodInline, ]

但是,当我遇到以下错误时,我似乎错误地指定了list_filter:

However I seem to have the list_filter incorrectly specified as I get the below error:

<class 'etariff.admin.SupplierAdmin'>: (admin.E116) The value of 'list_filter[1]' refers to 'payment_method__unique_id', which does not refer to a Field.


推荐答案

要使用相关的付款方式,您可以

To use the related payment methods, you can do

list_filter = ('paymentmethod',)

请注意没有下划线,请记住后面的逗号使其成为一个元组。

Note there is no underscore, and remember the trailing comma to make it a tuple.

如果要显示唯一的ID相关的付款方式,那么我认为您必须编写自己的 ListFilter 类。请参阅文档一个例子。

If you want to display the unique ids of related payment methods, then I think you'll have to write your own ListFilter class. See the docs for an example.

这篇关于管理员在反向相关字段上的django过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆