这是我的模特:
class UploadedOrderImage(models.Model):
order_image = models.OneToOneField(OrderImage)
image_large = models.ImageField(upload_to=get_file_path_random, height_field='order_image.image_large_height', width_field='order_image.image_large_width')
image_thumb = models.ImageField(upload_to=get_file_path_thumb, height_field='order_image.image_thumb_height', width_field='order_image.image_thumb_width')
其中order_image包含字段image_large_width
,image_large_height
,image_thumb_height
和image_thumg_width
。当我尝试上传图片时出现错误:
'UploadedOrderImage' has no attribute order_image.image_large_width
如何告诉django引用外键相关字段?
答案 0 :(得分:2)
'我不确定在这里使用外键属性。
但是这个怎么样?
class UploadedOrderImage(models.Model):
order_image = models.OneToOneField(OrderImage)
image_large = models.ImageField(upload_to=get_file_path_random)
image_thumb = models.ImageField(upload_to=get_file_path_thumb)
def save(self):
super(UploadedOrderImage, self).save()
if self.image_large:
image = Image.open(self.image_large)
size = (self.order_image.image_large_height,self.order_image.image_large_height)
image = image.resize(size, Image.ANTIALIAS)
image.save(self.image_large.path)
if self.image_thumb:
image = Image.open(self.image_thumb)
size = (self.order_image.image_thumb_height,self.order_image.image_thumb_height)
image = image.resize(size, Image.ANTIALIAS)
image.save(self.image_thumb.path)