Skip to content

Instantly share code, notes, and snippets.

@elyssonmr
Created January 27, 2017 01:26
Show Gist options
  • Save elyssonmr/1df158ff215b49c770904d04d2898b74 to your computer and use it in GitHub Desktop.
Save elyssonmr/1df158ff215b49c770904d04d2898b74 to your computer and use it in GitHub Desktop.
import urllib
from StringIO import StringIO
from django import forms
from django.core.files import File
from formexemplo.models import Pessoa
class PessoaModelForm(forms.ModelForm):
img_link = forms.CharField(max_length=250)
def save(self, commit=True):
pessoa = super(PessoaModelForm, self).save(commit=False)
url = self.cleaned_data['img_link']
pessoa.foto.save('teste.jpg', File(open(self.download_foto(url))))
if commit:
pessoa.save()
return pessoa
def download_foto(self, url):
resp = urllib.urlretrieve(url)
return resp[0]
class Meta:
model = Pessoa
fields = ['nome']
from __future__ import unicode_literals
from django.db import models
# Create your models here.
class Pessoa(models.Model):
foto = models.ImageField(upload_to='images/')
nome = models.CharField(max_length=50)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment