我有以下型号
class Category(models.Model):
name = models.CharField(max_length=30)
is_active=models.BooleanField()
photo=models.ImageField(upload_to='category')
def __unicode__(self):
name = str(self.name)
return name
class Meta:
permissions = (
('category','Category'),
('view_category', 'View category'),
)
我的表单类如下
class categoryForm(forms.Form):
name = forms.CharField(widget=forms.TextInput(attrs={'class':'box'}),max_length=32,label='Category',required=True)
is_active = forms.BooleanField(required=False)
id = forms.CharField(widget=forms.HiddenInput,required=False)
photo = forms.FileField(
required=False,
label='Select an Image',
help_text='max. 4 megabytes'
)
在 view.py 我有以下
formdata = categoryForm(request.POST,request.FILES)
if formdata.is_valid():
cd = formdata.cleaned_data
p1=Category()
p1.id=cd['id']
p1.name=cd['name']
p1.is_active=cd['is_active']
p1.photo=cd['photo']
p1.save()
它工作正常,但是当我将其更改为以下时,图像未上传
Category.objects.filter(id='%s'%(cd['id'])).update(name='%s'%(cd['name']),
is_active='%s'%(cd['is_active']),
photo=cd['photo']
)
我认为第二种方法更快,但为什么它不适用于 ImageField