安装 stdimage
pip install django-stdimage
配置settings.py
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'stdimage',
]
models.py
from stdimage.utils import UploadToUUID
class GoodsInfo(models.Model):
shopping_info = models.ForeignKey(LogisticsInfo, on_delete=models.SET_NULL,null=True,blank=True, verbose_name='订单号')
product_id = models.CharField(max_length=50, blank=True, null=True, verbose_name='产品id')
goods_name = models.CharField(max_length=255, blank=True, null=True, verbose_name='产品名')
image = StdImageField(max_length=100,
upload_to=UploadToUUID(path=datetime.now().strftime('banner/%Y/%m')),
verbose_name=u"图片",
variations={'thumbnail': {'width': 100, 'height': 75}})
sku_info = models.CharField(max_length=255, blank=True, null=True, verbose_name='产品属性')
sku_num = models.IntegerField(blank=True, null=True, verbose_name='产品数量')
sku_price = models.IntegerField(blank=True,null=True,verbose_name='商品单价')
add_time = models.DateTimeField(auto_now_add=True, verbose_name='添加时间')
class Meta:
verbose_name = '商品信息'
verbose_name_plural = verbose_name
db_table = 'goods_info'
def image_img(self):
if self.item_img:
return str('<img src="%s" />' % self.image.thumbnail.url)
else:
return u'图片'
image_img.short_description = '缩略图'
image_img.allow_tags = True
def __str__(self):
return self.product_id