如何将自定义查询/查询集添加到Django Admin?

时间:2020-04-07 17:35:24

标签: django django-models django-admin

我正在构建一个Django项目,并有一个名为Animal的模型。像这样:

class Animal(models.Model):
    type = models.CharField()

在django管理界面中,我可以轻松看到所有很棒的动物

不过,我想向管理员添加2个其他视图,以便可以看到特定类型的所有动物

例如,如果我的管理员视图看起来像这样:

Animals -> Shows all animals (default)
Animals (type = dog) -> Shows only dogs
Animals (type = cat) -> Shows only cats

有关如何执行此操作的任何建议或最佳做法?谢谢。查询本身将非常简单

Animals.objects.filter(type='dog')

1 个答案:

答案 0 :(得分:0)

我相信您正在寻找list_filter配置属性。

Select Case Target.Value2
Case is = "Aggr"  'selected Aggr in dropdown
    For Each Srs In oChart.SeriesCollection
        If Srs.Name = "Core" Then
            Var = Split(Srs.Formula, ",")
            Set Rng = ThisWorkbook.Sheets("Sheet1").Range(Var(2)).Offset(0, -2)
            Var(2) = Trim("'" & Mid(Rng.Address(True, True, xlA1, True), InStr(1, Rng.Address(True, True, xlA1, True), "]", vbTextCompare) + 1))
            With Srs
                .Formula = Join(Var, ",")
                .Name = "Aggr"
            End With
            Exit For
        End If
    Next Srs
    Target.Select

Case is = "Core"  'selected Core in dropdown
    For Each Srs In oChart.SeriesCollection
        If Srs.Name = "Aggr" Then
            Var = Split(Srs.Formula, ",")
            Set Rng = ThisWorkbook.Sheets("Sheet1").Range(Var(2)).Offset(0, 2)
            Var(2) = Trim("'" & Mid(Rng.Address(True, True, xlA1, True), InStr(1, Rng.Address(True, True, xlA1, True), "]", vbTextCompare) + 1))
            With Srs
                .Formula = Join(Var, ",")
                .Name = "Core"
            End With
            Exit For
        End If
    Next Srs
    Target.Select
End Select