【发布时间】:2012-08-01 07:49:03
【问题描述】:
我正在尝试使用 django orm 仅更新图像字段,即用户稍后将添加图像并将在主表中更新
我试过了
MerchantProfile.objects.filter(id='%s'%(cd['id'])).update(photo='%s'%(cd['photo']))
但它只会更新标题,而不是保存在文件夹中的图像
然后我尝试关注
p1=MerchantProfile()
p1.id=cd['id']
if cd['photo']:
p1.photo=cd['photo']
else:
p1.photo=cd['uploaded_photo']
p1.save()
它将保存图像,但会显示错误,例如 payment_card cannot be null
我的模型如下
class Merchantprofile:
user = models.OneToOneField(UserProfile, related_name="merchant_profile")
payment_card = models.OneToOneField(PaymentCard, related_name="merchant_profile")
current_state = models.IntegerField('State', choices=STATE_CHOICES)
name = models.CharField('Merchant Name', max_length=64)
photo=models.ImageField(upload_to='logo',blank=True)
表格
class LogoForm(forms.Form):
id = forms.CharField(widget=forms.HiddenInput,required=False)
photo = forms.ImageField(
required=False,
label='Upload photo',
initial=True,
help_text='max. 4 megabytes'
)
那么只有在相关文件夹上上传照片才能更新照片字段的最佳方法是什么,因为我不能每次都更新所有字段
【问题讨论】: