Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to reference a index field from fk model in admin

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

Try this in mark_safe function:

ProductImages.objects.filter(product=obj).first().image_file.url
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading