Created
January 27, 2017 01:26
-
-
Save elyssonmr/1df158ff215b49c770904d04d2898b74 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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