我想thumbnail在管理模型中創建一個。現在我有:
模型.py
class ProductImages(models.Model):
product = models.ForeignKey(Product, on_delete=models.CASCADE)
image_type = models.CharField(max_length=33,default='image_type')
image_file = models.ImageField(
upload_to='images/',
null=True,
blank=True,
default='magickhat-profile.jpg'
)
管理檔案
class AdminProductModel(admin.ModelAdmin):
model = Product
def product_thumb(self, obj):
# return HTML link that will not be escaped
return mark_safe(
'<img src="%s" style="width:30px;height:30px;">' % (ProductImages.image_file.url)"""here is my doubt, i need only 1 image"""
)
list_display = ['product_thumb','user','slug','created_at']
...
好吧,ProductImages模型存盤與Product模型相關的所有影像,我需要獲取 1 張與thumbinail管理員相關的影像
uj5u.com熱心網友回復:
在mark_safe函式中試試這個:
ProductImages.objects.filter(product=obj).first().image_file.url
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/395947.html
標籤:姜戈
