mirror of
https://github.com/SectorLabs/django-localized-fields.git
synced 2025-04-25 11:42:54 +03:00
Use template-based widget rendering in AdminLocalizedFieldWidget
This commit is contained in:
parent
d8c5544e91
commit
69cf0df166
@ -48,6 +48,38 @@ class LocalizedFieldWidget(forms.MultiWidget):
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
def get_context(self, name, value, attrs):
|
||||||
|
context = super(forms.MultiWidget, self).get_context(name, value, attrs)
|
||||||
|
if self.is_localized:
|
||||||
|
for widget in self.widgets:
|
||||||
|
widget.is_localized = self.is_localized
|
||||||
|
# value is a list of values, each corresponding to a widget
|
||||||
|
# in self.widgets.
|
||||||
|
if not isinstance(value, list):
|
||||||
|
value = self.decompress(value)
|
||||||
|
|
||||||
|
final_attrs = context['widget']['attrs']
|
||||||
|
input_type = final_attrs.pop('type', None)
|
||||||
|
id_ = final_attrs.get('id')
|
||||||
|
subwidgets = []
|
||||||
|
for i, widget in enumerate(self.widgets):
|
||||||
|
if input_type is not None:
|
||||||
|
widget.input_type = input_type
|
||||||
|
widget_name = '%s_%s' % (name, i)
|
||||||
|
try:
|
||||||
|
widget_value = value[i]
|
||||||
|
except IndexError:
|
||||||
|
widget_value = None
|
||||||
|
if id_:
|
||||||
|
widget_attrs = final_attrs.copy()
|
||||||
|
widget_attrs['id'] = '%s_%s' % (id_, i)
|
||||||
|
else:
|
||||||
|
widget_attrs = final_attrs
|
||||||
|
widget_attrs = self.build_widget_attrs(widget, widget_value, widget_attrs)
|
||||||
|
subwidgets.append(widget.get_context(widget_name, widget_value, widget_attrs)['widget'])
|
||||||
|
context['widget']['subwidgets'] = subwidgets
|
||||||
|
return context
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def build_widget_attrs(widget, value, attrs):
|
def build_widget_attrs(widget, value, attrs):
|
||||||
attrs = dict(attrs) # Copy attrs to avoid modifying the argument.
|
attrs = dict(attrs) # Copy attrs to avoid modifying the argument.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user