copy file from one model to another

后端 未结 5 1727
孤城傲影
孤城傲影 2020-12-09 03:34

I have 2 simple models:

class UploadImage(models.Model):
   Image = models.ImageField(upload_to=\"temp/\")

class RealImage(models.Model):
   Image = models.         


        
5条回答
  •  半阙折子戏
    2020-12-09 04:06

    Try doing that without using a form. Without knowing the exact error that you are getting, I can only speculate that the form's clean() method is raising an error because of a mismatch in the upload_to parameter.

    Which brings me to my next point, if you are trying to copy the image from 'temp/' to 'real/', you will have to do a some file handling to move the file yourself (easier if you have PIL):

    import Image
    from django.conf import settings
    
    u = UploadImage.objects.get(id=image_id)
    im = Image.open(settings.MEDIA_ROOT + str(u.Image))
    newpath = 'real/' + str(u.Image).split('/', 1)[1]
    im.save(settings.MEDIA_ROOT + newpath)
    r = RealImage.objects.create(Image=newpath)
    

    Hope that helped...

提交回复
热议问题