from django import forms
from django.contrib import admin
from django.utils.html import format_html
from django.utils.safestring import mark_safe
from shop.models import Customer
class MessageRenderer(forms.Textarea):
def render(self, name, value, attrs=None, renderer=None):
if not value:
return super().render(name, value, attrs, renderer)
else:
html = '{}' + forms.HiddenInput().render(name, value)
value = self.format_value(value).replace('\n', '<br>')
return format_html(html, mark_safe(value))
class MyForm(forms.ModelForm):
class Meta:
model = Customer
fields = ['author', 'body']
widgets = {
'body': MessageRenderer(attrs={'cols': '80', 'rows': '15'}),
}
@admin.register(Customer)
class CustomerAdmin(admin.ModelAdmin):
form = MyForm