在Django模型中保存时自动生成图像字段

[英]Autopopulate Imagefield in a Django model upon saving


I currently have this:

我现在:

class youtube_video(models.Model):
   video_url = models.CharField(max_length=150,blank=True, validators=[RegexValidator("^.*((v\/)|(embed\/)|(watch\?))\??v?=?(?P<vid_id>[^\&\?]*).*")])
   video_thumbnail = models.ImageField(upload_to="/thumbs")

   def save(self, args*, kwargs**):
      video_thumbnail = urllib2.urlretrieve(#trivial regex here that gets the thumbnail url from the video_url)
      super(youtube_video, self).save(*args, **kwargs)

This isn't working, but hopefully it demonstrates what I'm trying to do. Essentially I want to autopopulate the video_thumbnail ImageField upon the model saving, using another field in the model.

这行不通,但希望它能证明我正在做的。本质上,我希望在模型保存时使用模型中的另一个字段自动生成video_thumbnail ImageField。

2 个解决方案

#1


0  

My answer is heavily based on the previous answer, but it didn't work as presented, so here are two versions that should work:

我的回答很大程度上是基于之前的回答,但它并没有像前面提到的那样起作用,所以这里有两个版本应该是可行的:

from django.core.files import File
from django.core.files.temp import NamedTemporaryFile
import urllib2

class SomeModel(models.Model):
    # Some attributes...
    image = models.ImageField(upload_to='dir')

      def save(self, *args, **kwargs):
        url = 'http://www.path.to.image.com/image.jpg'
        img_temp = NamedTemporaryFile(delete=True)
        img_temp.write(urllib2.urlopen(url).read())
        img_temp.flush()
        self.image.save('name_of_your_image.jpg',File(img_temp),save=False)
        super(SomeModel, self).save(*args, **kwargs)

If you don't care about the name of the image being saved on your media folder, you can do:

如果您不关心保存在您的媒体文件夹中的图像的名称,您可以这样做:

from django.core.files import File
from django.core.files.temp import NamedTemporaryFile
import urllib2

class SomeModel(models.Model):
    # Some attributes...
    image = models.ImageField(upload_to='dir')

      def save(self, *args, **kwargs):
        url = 'http://www.path.to.image.com/image.jpg'
        img_temp = NamedTemporaryFile(delete=True)
        img_temp.write(urllib2.urlopen(url).read())
        img_temp.flush()
        self.image = File(img_temp)
        super(SomeModel, self).save(*args, **kwargs)

#2


2  

Remember you need to reference self from within instance methods.

请记住,您需要从实例方法中引用self。

def save(self, args*, kwargs**):
      self.video_thumbnail = urllib2.urlretrieve(...)
      super(youtube_video, self).save(*args, **kwargs)

However there's still a problem. urlretrieve returns a (filename, headers) tuple, not a valid File object.

然而,仍然存在一个问题。urlretrieve返回一个(文件名,header)元组,而不是一个有效的文件对象。

See this question on how to retrieve a file for an ImageField.

请参见关于如何为ImageField检索文件的问题。

Edit:

编辑:

from django.core.files import File
from django.core.files.temp import NamedTemporaryFile

def save(self, args*, kwargs**):
    img_temp = NamedTemporaryFile(delete=True)
    img_temp.write(urllib2.urlopen(..regex..).read())
    img_temp.flush()
    file_name = 'determine filename'
    self.video_thumbnail.save(file_name, File(img_temp))
    super(youtube_video, self).save(*args, **kwargs)

The above is based on the answer linked above, and reading about FileField in the Django documentation. I haven't had to work with FileFields myself, so I hope this is helpful.

以上是基于上面链接的答案,并在Django文档中阅读关于FileField的内容。我自己不需要使用FileFields,所以我希望这对您有帮助。

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2011/03/13/5368a2232bc25519af882ad45e948b1a.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告