在admin.py中注册模型管理列表

时间:2017-10-09 20:37:27

标签: python django-models django-admin

我有我在管理页面中显示的模型

admin.py

from django.contrib import admin
from jet.filters import DateRangeFilter
from .models import KPIReport
# from date_range_filter import DateRangeFilter
from django.utils.html import format_html
# from rangefilter.filter import DateRangeFilter
# Register your models here.



@admin.register(KPIReport)
class KPIReportAdmin(admin.ModelAdmin):


    list_display = ('Date','Item_SKU','Account','Country','Sessions','Session_Pct','Page_Views','Page_Views_Pct','Buy_Box_Pct','Units_Ordered','Unit_Session_Pct','Ordered_Product_Sales','Total_Order_Items','Sales_Rank','Actual_Sales','Selling_Price','Reviews','Camel', 'Notes')
    list_filter = (('Date', DateRangeFilter),'ItemSKU','Account','Country')
    search_fields = ('Date','ASIN','ItemSKU','Account','Country','Selling_Price')
    list_editable = ('Notes',)

我想在我的管理员中生成第二个表,按帐户过滤相同的模型。在这种情况下CHG

class KPI_CHG(admin.ModelAdmin):
    model = KPIReport

    def get_queryset(self, request):
        qs = super(KPI_CHG, self).get_queryset(request)
        return qs.filter(Account='CHG')

    list_display = ('Date','Item_SKU','Account','Country','Sessions','Session_Pct','Page_Views','Page_Views_Pct','Buy_Box_Pct','Units_Ordered','Unit_Session_Pct','Ordered_Product_Sales','Total_Order_Items','Sales_Rank','Actual_Sales','Selling_Price','Reviews','Camel', 'Notes')
    list_filter = (('Date', DateRangeFilter),'ItemSKU','Country')
    search_fields = ('Date','ASIN','ItemSKU','Country','Selling_Price')
    list_editable = ('Notes',)

我知道我有过滤器,我可以下载并完成此操作。用户仍希望能够转到此列表的版本,该列表仅包含来自帐户“CHG”的对象

使用1.10和python 2.7

如何查看第二张表,或者我的方法不正确?

1 个答案:

答案 0 :(得分:0)

您可以做的是将代理模型与自定义管理器一起使用 Link to Docs。这是必要的,因为您只能注册一次模型。使用自定义管理器,您可以使用相同的数据库表,但行为略有不同

在您的情况下,您必须在models.py中执行以下操作:

read.csv()

在admin.py中:

class CHG_Manager(models.Manager):
    def get_queryset(self):
        return super().get_queryset().filter(Account='CHG')

class KPI_CHG(KPIReport):
    objects = CHG_Manager()
    class Meta:
        proxy = True

希望它有所帮助!

相关问题