이 사진 모델은장고에서 너비와 높이를 캡처하는 필드와 필드가있는 모델을 프로그래밍 방식으로 어떻게 만들 수 있습니까?
입니다.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 객체가 있는가? 내가 쓸 수있어?
나는 형식을 볼 수 있었다고 생각합니다. 이미지 필드가 정리합니다. 그게 도움이되는지 알려주지. – arustgi