from django.contrib import admin
from .models import Product
from django.utils.html import format_html
# Register your models here.
class ProductAdmin(admin.ModelAdmin):
list_display = ('name', 'price', 'styled_stock')
def styled_stock(self, obj):
if obj.stock <= 50:
return format_html(f'<span style="color:red">{obj.stock}</span>')
return format_html(f'<span>{obj.stock}</span>')
styled_stock.short_description = '재고'
admin.site.register(Product, ProductAdmin)
docs : https://docs.djangoproject.com/en/3.1/ref/contrib/humanize/
from django.contrib import admin
from .models import Product
from django.utils.html import format_html
from django.contrib.humanize.templatetags.humanize import intcomma
# Register your models here.
class ProductAdmin(admin.ModelAdmin):
list_display = ('name', 'price_format', 'styled_stock')
def price_format(self, obj):
price = intcomma(obj.price)
return f'{price} 원'
def styled_stock(self, obj):
if obj.stock <= 50:
return format_html(f'<span style="color:red">{obj.stock}</span>')
return format_html(f'<span>{obj.stock}</span>')
price_format.short_description = '가격'
styled_stock.short_description = '재고'
admin.site.register(Product, ProductAdmin)
from django.contrib import admin
from .models import Product
from django.utils.html import format_html
from django.contrib.humanize.templatetags.humanize import intcomma
# Register your models here.
class ProductAdmin(admin.ModelAdmin):
list_display = ('name', 'price_format', 'styled_stock')
def price_format(self, obj):
price = intcomma(obj.price)
return f'{price} 원'
def styled_stock(self, obj):
stock = obj.stock
if stock <= 50:
stock = intcomma(stock)
return format_html(f'<b style="color:red">{stock} 개</b>')
return f'{intcomma(stock)} 개'
price_format.short_description = '가격'
styled_stock.short_description = '재고'
admin.site.register(Product, ProductAdmin)