过滤Django管理员选择框的模型结果 [英] Filtering model results for Django admin select box

查看:95
本文介绍了过滤Django管理员选择框的模型结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始玩Django,到目前为止,我发现很难做简单的事情。我正在努力的是过滤状态类型列表。 StatusTypes模型是:

I just started playing with Django today and so far am finding it rather difficult to do simple things. What I'm struggling with right now is filtering a list of status types. The StatusTypes model is:

class StatusTypes(models.Model):
    status = models.CharField(max_length=50)
    type = models.IntegerField()
    def __unicode__(self):
        return self.status
    class Meta:
        db_table = u'status_types'

在一个管理页面中,我需要所有的结果,其中type = 0,在另一个我需要所有的结果, type = 1,所以我不能仅仅从模型中限制它。我该怎么做?

In one admin page I need all the results where type = 0 and in another I'll need all the results where type = 1 so I can't just limit it from within the model. How would I go about doing this?

编辑:我应该有点更清楚了我有一个模型Unit,它有一个外键到StatusTypes。模型如下:

I should have been a bit more clear. I have a model "Unit" which has a foreign key to to StatusTypes. The models are as follows:

class StatusTypes(models.Model):
    status = models.CharField(max_length=50)
    type = models.IntegerField()
    def __unicode__(self):
        return self.status
    class Meta:
        db_table = u'status_types'

class Unit(models.Model):
    name = models.CharField(unique=True, max_length=50)
    status = models.ForeignKey(StatusTypes, db_column='status')
    note = models.TextField()
    date_added = models.DateTimeField()
    def __unicode__(self):
        return self.name
    class Meta:
        db_table = u'units'

所以现在在单位模型的管理页面我想限制状态只有类型= 1的那些。基于以下的lazerscience反应,我尝试了以下代码:

So now in the admin page for the unit model I want to limit the status to only those with type = 1. Based off of lazerscience response below I tried the following code:

from inv.inventory.models import Unit
from django.contrib import admin

class UnitAdmin(admin.ModelAdmin):
    def queryset(self, request):
        qs = super(UnitAdmin, self).queryset(request)
        return qs.filter(type=0)

admin.site.register(Unit, UnitAdmin)

但是,它没有不要改变选择框。我也尝试打印qs的值,没有任何东西输出到我的终端,所以我想知道我是否需要一些如何调用queryset?

But, it didn't change the select box at all. I also tried printing the value of qs and nothing was outputted to my terminal so I'm wondering if I have to some how call queryset?

编辑2: 可能不清楚,我想过滤此单位模型的创建页面上的状态下拉列表。

EDIT 2: It might not have been clear that I want to filter this for the status dropdown that is on the create page for the Unit model.

推荐答案

编辑:

事实证明,在这种情况下,ModelAdmin.formfield_for_foreignkey是正确的答案:
http://docs.djangoproject.com/en/dev/ref /contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey

It turns out that ModelAdmin.formfield_for_foreignkey was the right answer in this situation: http://docs.djangoproject.com/en/dev/ref/contrib/admin/#django.contrib.admin.ModelAdmin.formfield_for_foreignkey

以前的答案:

查看 ModelAdmin的list_filter属性。这听起来更像你想要的,因为它将创建一个漂亮的界面来过滤不同的标准,而不是任意限制你的查询。

Take a look at the list_filter attribute of ModelAdmin. That sounds more like what you want to me since it will create a nice interface for filtering on different criteria rather than arbitrarily restricting your queryset.

这篇关于过滤Django管理员选择框的模型结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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