我想根据复选框选择添加多个复选框以在视图中过滤花朵。我在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)
答案 0 :(得分: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
答案 1 :(得分:0)
有些事情是这样的:
在views.py中:
# get the selected flower id as fid
FlowerForm = create_flower_form(fid)
form.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