Image를 Admin페이지 내 표시
models.py
class Product(models.Model):
name = models.CharField(max_length=250)
image = models.ImageField(upload_to='products', blank=True)
price = models.PositiveIntegerField()
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
admin.py
- list_display
- 모델에서는 image필드라고 정의했지만 photo_tag() 메서드로 오버라이딩하여 사용
from django.utils.safestring import mark_safe
@admin.register(Product)
class ProductionAdmin(admin.ModelAdmin):
list_display = ['photo_tag', 'name', 'price']
list_display_links = ['name']
def photo_tag(self, item):
if item.image:
return mark_safe('<img src={} style="width: 75px;"/>'.format(item.image.url))
return None
photo_tag.short_description = 'Image'
[참고블로그]
'Python > Django' 카테고리의 다른 글
Custom Manager, QuerySet (0) | 2022.04.05 |
---|---|
[Django] rest-auth, allauth 차이 (0) | 2021.01.18 |
[Django] Admin page 수정하기 (1) (0) | 2021.01.16 |
[Django] models - class Meta (0) | 2021.01.12 |
[Django] 직접 만든 코드 Django에서 사용하기. (0) | 2020.12.30 |
댓글