Use renderer kwarg for picklefield widget; related to #1843

This commit is contained in:
Griatch 2019-06-03 22:40:32 +02:00
parent 3ef51823d6
commit 5064242ef8

View file

@ -107,8 +107,9 @@ def dbsafe_decode(value, compress_object=False):
class PickledWidget(Textarea): class PickledWidget(Textarea):
def render(self, name, value, attrs=None): def render(self, name, value, attrs=None, renderer=None):
"""Display of the PickledField in django admin""" """Display of the PickledField in django admin"""
value = repr(value) value = repr(value)
try: try:
# necessary to convert it back after repr(), otherwise validation errors will mutate it # necessary to convert it back after repr(), otherwise validation errors will mutate it
@ -121,11 +122,18 @@ class PickledWidget(Textarea):
attrs["name"] = name attrs["name"] = name
else: else:
attrs = {"name": name} attrs = {"name": name}
attrs['cols'] = 30
# adapt rows to width
rows = 1
if isinstance(value, str) and "\n" in value:
rows = max(1, len(value.split('\n')))
attrs['rows'] = rows
attrs = self.build_attrs(attrs)
final_attrs = self.build_attrs(attrs) return super().render(name, value, attrs=attrs, renderer=renderer)
return format_html('<textarea{0}>\r\n{1}</textarea>', # return format_html('<textarea{0}>\r\n{1}</textarea>',
flatatt(final_attrs), # flatatt(final_attrs),
value) # value)
class PickledFormField(CharField): class PickledFormField(CharField):
@ -138,6 +146,7 @@ class PickledFormField(CharField):
"surrounded by quote marks. We have converted it to a string for your " "surrounded by quote marks. We have converted it to a string for your "
"convenience. If it is acceptable, please hit save again.") "convenience. If it is acceptable, please hit save again.")
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
# This needs to fall through to literal_eval. # This needs to fall through to literal_eval.
kwargs['required'] = False kwargs['required'] = False