0

我想根据复选框选择添加多个复选框以过滤视图中的花朵。我对 Django 非常陌生。如何修改我的 models.py ?

from django.db import models

class Flower(models.Model):
    name = models.CharField(max_length = 30)
    price = models.DecimalField(max_digits=6, decimal_places=2)
    details = models.TextField()

    def __unicode__(self):
        return self.name
    def my_upload_to(instance, filename):
        return 'static/media/' + instance.name + '.jpg'
    image = models.ImageField(upload_to=my_upload_to)
4

2 回答 2

0

像这样的东西:

在views.py中:

# get the selected flower id as fid
FlowerForm = create_flower_form(fid)

在forms.py中:

def create_flower_form(id):
class fform(forms.ModelForm):

    name = forms.ModelChoiceField(label='Flower type ',
            queryset=FLower.objects.filter(id=fid))


    class Meta:

        model = Flower

return fform
于 2013-08-21T10:11:45.667 回答
0

您可以使用表单完成,这是代码:

from django.forms import widgets
class FlowerForm(forms.ModelForm):
    name = forms.ModelMultipleChoiceField(label='Name',queryset=Flower.objects.all(),
           widget=forms.CheckboxSelectMultiple)
    class Meta:
        model = Flower
于 2013-08-21T09:18:31.610 回答