Как ссылаться на индексное поле из fk-модели в админке

Я хочу сделать thumbnail в модели администратора. На данный момент у меня есть:

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

Ну, модель ProductImages хранит все изображения, связанные с моделью Product, и мне нужно получить 1 изображение, связанное с сделанным thumbinail в админке

Попробуйте это в mark_safe функции:

ProductImages.objects.filter(product=obj).first().image_file.url
Вернуться на верх