django中的动态过滤器选择字段

时间:2015-01-13 22:15:06

标签: python django django-filter

我正在使用django-filter来过滤网页上的结果。我能够使用这样的静态选择过滤器:

filters.py

FILTER_CHOICES = (
('', 'All States'),
('AL', 'Alabama'),
('AK', 'Alaska'),
('AZ', 'Arizona'),
#and so forth, I am not going to list all 50 states for this a question on a forum
)


class SightingFilter(django_filters.FilterSet):
    state = django_filters.ChoiceFilter(choices=FILTER_CHOICES)
    class Meta:
        model = Sighting
        fields = ['city', 'store', 'brand', 'product', 'flavor', 'fat', 'size']   

所以上面的代码工作正常,但是如果我尝试在下面创建一个像“plant_number”这样的动态列表:

class SightingFilter(django_filters.FilterSet):
    state = django_filters.ChoiceFilter(choices=FILTER_CHOICES)
    plant_number = django_filters.ChoiceFilter(choices=[(o.plant_number, o.plant_number + " " + o.manufacturer_name) for o in Plant.objects.all()])
 class Meta:
        model = Sighting
        fields = ['city', 'store', 'brand', 'product', 'flavor', 'fat', 'size']

然后我收到错误:

基数为10的int()的文字无效:

它在我的模板中突出显示了这段代码

{% for obj in filter %}
    <a href="/sighting/{{ obj.slug }}/ ">{{ obj.date|date:"m/d/Y" }} {{ obj.brand }} ${{ obj.price|intcomma }} </a><br />
{% endfor %}

plant_number是模型中的CharField(工厂编号使用格式XX-XXX,其中X是数字)。

我的观点如下:

def dashboard(request):
    if "Clear Filters" in request.GET:
        return redirect('/')
else:
    filter = SightingFilter(request.GET, queryset=Sighting.objects.all())
    context = {'filter': filter, 'request': request}
    return render_to_response('dashboard.html', context, context_instance=RequestContext(request))

感谢您的帮助!

编辑1/14/15

所以我很确定问题是plant_number是一个外键(抱歉不包括上面的models.py)。这就是为什么它期望一个int而不是一个字符串。所以现在我需要弄清楚如何获取工厂编号的外键值而不仅仅是工厂编号。我试过了:

plant_number = django_filters.ChoiceFilter(choices=[[o.plant_number.id, o.plant_number + " " + o.Manufacturer] for o in Plant.objects.all()])

也尝试过:

plant_number = django_filters.ChoiceFilter(choices=[[o.plant_number_id, o.plant_number + " " + o.Manufacturer] for o in Plant.objects.all()])

两者都不起作用。我得到'unicode'对象在第一个上没有属性'id','Plant'对象在第二个上没有属性'plant_number_id'。

models.py

class Sighting(models.Model):
    date = models.DateField(default=today)
    brand = models.CharField(max_length=200)
    product = models.CharField(max_length=200)
    flavor = models.CharField(max_length=200)
    fat = models.DecimalField(max_digits=4, decimal_places=2)
    size = models.DecimalField(max_digits=10, decimal_places=2)
    price = models.DecimalField(max_digits=10, decimal_places=2)
    plant_number = models.ForeignKey('Plant', blank=True, null=True )
    store = models.CharField(max_length=200)
    city =  models.CharField(max_length=200)
    state = models.CharField(max_length=200, choices=STATE_CHOICES)
    photo = ProcessedImageField(upload_to=yogurtupload,
                                   processors=[ResizeToFit(600, 600)], #Resizes so the largest dimension is 600 (width or height, whichever hits 600 first)
                                   format='JPEG',
                                   options={'quality': 72})
    slug = models.SlugField(unique=True)

    def save(self):
        now = timestamp()
        forslug = str(now) + " " + str(self.plant_number) + " " + str(self.brand)
        self.slug = slugify(forslug)
        super(Sighting, self).save()

     def __unicode__(self):
        return self.slug

class Plant(models.Model):
    plant_number = models.CharField(max_length=200)
    Manufacturer = models.CharField(max_length=200)
    city = models.CharField(max_length=200)
    state = models.CharField(max_length=200, choices=STATE_CHOICES)
    slug = models.SlugField(unique=True)

    def save(self):
        self.slug = slugify(self.plant_number)
        super(Plant, self).save()

    def __unicode__(self):
        return self.slug

2 个答案:

答案 0 :(得分:2)

那是因为FILTER_CHOICES是元组中的元组,而不是列表中的元组。试试这个:

plant_number = django_filters.ChoiceFilter(choices=((o.plant_number.id, o.plant_number.foobar + " " + o.manufacturer_name) for o in Plant.objects.all()))

答案 1 :(得分:0)

所以我想通了,我传递的是 plant_number 而不是 id ,这是django所期待的,因为我的过滤器是针对Sightings模型的,而且是 plant_number 是Sightings模型中的外键。有效的代码:

plant_number = django_filters.ChoiceFilter(choices=[[o.id, o.plant_number + " " + o.Manufacturer] for o in Plant.objects.all().order_by('IMS_plant')])