简体   繁体   中英

update in django with image field

I am trying to use django orm to update image field only ie user will later add image and will be updated in primary table

i tried

MerchantProfile.objects.filter(id='%s'%(cd['id'])).update(photo='%s'%(cd['photo']))

but it will update the caption only and image in not saved in folder

Then i tried following

p1=MerchantProfile()
p1.id=cd['id']
if cd['photo']:
       p1.photo=cd['photo']
else:
       p1.photo=cd['uploaded_photo']
p1.save()

It will save the image but will show error like payment_card cannot be null

My models is as below

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)

form

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'
    )

so what is the best way to update photo field only with uploading photo on relevant folder, as i cannot update all field everytime

Try this:

p1 = MerchantProfile.objects.filter(id=cd['id'])
if cd['photo']:
   p1.photo=cd['photo']
else:
   p1.photo=cd['uploaded_photo']
p1.save()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM