2011-09-02 4 views
1

이 사진 모델은장고에서 너비와 높이를 캡처하는 필드와 필드가있는 모델을 프로그래밍 방식으로 어떻게 만들 수 있습니까?

입니다.
class Photo(models.Model): 
    title = models.CharField(max_length=100, blank=True) 
    width = models.IntegerField() 
    height = models.IntegerField() 
    image = models.ImageField(upload_to=upload_to_callable, width_field="width", height_field="height") 
    content_type = models.ForeignKey(ContentType) 
    object_id = models.PositiveIntegerField() 
    content_object = generic.GenericForeignKey() 
    create_date = models.DateTimeField(auto_now_add=True) 
    modified_date = models.DateTimeField(auto_now=True) 
    site = models.ForeignKey(Site) 

제 테스트 중 하나에서이 모델을 프로그래밍 방식으로 생성해야합니다. 이게 내가하고있는 일이다.

def _create_random_image(self): 
     import Image,ImageDraw 

     img = Image.new("RGB", (300,300), "#FFFFFF") 
     draw = ImageDraw.Draw(img) 

     r,g,b = rint (0,255), rint(0,255), rint(0,255) 
     dr = (rint(0,255) - r)/300. 
     dg = (rint(0,255) - g)/300. 
     db = (rint(0,255) - b)/300. 
     for i in range(300): 
      r,g,b = r+dr, g+dg, b+db 
      draw.line((i,0,i,300), fill=(int(r),int(g),int(b))) 

     tmpfilename = "/tmp/test36052.png" 
     img.save(tmpfilename, "PNG") 
     f = file(tmpfilename) 
     return f 

def testHomePageImage(self): 
    tmpfile = self._create_random_image() 
    photo = Photo.objects.create(image=tmpfile, 
           title=site1_home_page.title, 
           site=self.site1, 
           content_object = site1_home_page) 

AttributeError : 'file'객체에 'width'속성이 없다.

파일 객체를 확장하는 내부 django 객체가 있는가? 내가 쓸 수있어?

+0

나는 형식을 볼 수 있었다고 생각합니다. 이미지 필드가 정리합니다. 그게 도움이되는지 알려주지. – arustgi

답변

1

f 대신 ImageFile(f)을 직접 돌려 보내보십시오. ImageFiledjango.core.files.images.ImageFile입니다. 내 머리 꼭대기에서, 나는 그것을 할 것이라고 생각합니다.

편집 : 오타가 수정되었으므로 정확합니다.

+0

아주 가까이 .. 내가 ImageFile (F) 사진 = Photo.objects.create (이미지 = ImageFile (TMPFILE), 제목 = site1_home_page.title, 사이트 = self.site1, content_object = site1_home_page) – arustgi

+0

찰흙을해야했다! 수업을 들여 놓았을 때 나는 단지 타이포드였습니다. 제 수입은 정확했습니다. :-디 – tswicegood

관련 문제