2

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.

Tony
  • 125
  • 1
  • 1
  • 6

2 Answers2

2

Remember you need to reference self from within instance methods.

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.

See this question on how to retrieve a file for an 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.

Community
  • 1
  • 1
Josh Smeaton
  • 47,939
  • 24
  • 129
  • 164
  • Sadly still not working. I think urlretrieve is itself ok, however i'm not sure that `self.video_thumbnail` can take an `urllib2.urlretrieve` object. – Tony Mar 13 '11 at 04:25
  • still having issue. I wonder if handling this in the ModelForm might be a better idea? – Tony Mar 13 '11 at 07:11
  • It would be a better idea, unless you need to add instances without the use of a form. Though I don't think using a form is going to magically make the way you assign the thumb_nail field work. You need to create an instance of a File as explained in the linked question. – Josh Smeaton Mar 13 '11 at 07:30
  • Well I won't need to add instances without a form, so that's good to know. As far as the link, I tried to duplicate the code but kept getting errors. This might just be a bit beyond my beginner's understanding of Django. – Tony Mar 13 '11 at 07:58
  • Unfortunately still no luck. I think this might be a lost cause at my current skill level, but seriously thank you so so much for the help. One day I'll pick up this line of thinking and get it right thanks to your help. – Tony Mar 14 '11 at 06:17
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)
Community
  • 1
  • 1
João Pesce
  • 2,424
  • 1
  • 24
  • 26