Home > Software design >  How to reference a index field from fk model in admin
How to reference a index field from fk model in admin

Time:12-28

I want to made a thumbnail in admin model. For now i have:

models.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'
    )

admin.py

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']
    ...

Well, the ProductImages model store all images related with Product model, and i need get 1 image related to made thumbinail in admin

CodePudding user response:

Try this in mark_safe function:

ProductImages.objects.filter(product=obj).first().image_file.url
  • Related