mirror of
https://github.com/Hopiu/django-select2.git
synced 2026-03-16 21:40:23 +00:00
Better fix for registering field_id
This commit is contained in:
parent
4e240ce415
commit
4479cfbdf7
2 changed files with 8 additions and 9 deletions
|
|
@ -11,7 +11,7 @@ class AutoViewFieldMixin(object):
|
|||
|
||||
from . import util
|
||||
id_ = util.register_field(name, self)
|
||||
self.widget.field_id = id_
|
||||
self.field_id = id_
|
||||
super(AutoViewFieldMixin, self).__init__(*args, **kwargs)
|
||||
|
||||
def security_check(self, request, *args, **kwargs):
|
||||
|
|
@ -199,6 +199,13 @@ class HeavySelect2FieldBase(ChoiceMixin, forms.Field):
|
|||
|
||||
kargs.update(kwargs)
|
||||
super(HeavySelect2FieldBase, self).__init__(*args, **kargs)
|
||||
# This piece of code is needed here since (God knows) why Django's Field class does not call
|
||||
# super(); because of that __init__() of classes would get called after Field.__init__().
|
||||
# If did had super() call there then we could have simply moved AutoViewFieldMixin at the
|
||||
# end of the MRO list. This way it would have got widget instance instead of class and it
|
||||
# could have directly set field_id on it.
|
||||
if hasattr(self, 'field_id'):
|
||||
self.widget.field_id = self.field_id
|
||||
|
||||
class HeavySelect2ChoiceField(HeavySelect2FieldBase):
|
||||
widget = HeavySelect2Widget
|
||||
|
|
|
|||
|
|
@ -208,14 +208,6 @@ class HeavySelect2MultipleWidget(HeavySelect2Mixin, MultipleSelect2HiddenInput):
|
|||
### Auto Heavy widgets ###
|
||||
|
||||
class AutoHeavySelect2Mixin(object):
|
||||
def __init__(self, *args, **kwargs):
|
||||
if hasattr(self.__class__, 'field_id'): # By the time AutoViewFieldMixin runs widget is not instantiated
|
||||
# so it sets the value on the widget class.
|
||||
self.field_id = getattr(self.__class__, 'field_id')
|
||||
delattr(self.__class__, 'field_id')
|
||||
|
||||
super(AutoHeavySelect2Mixin, self).__init__(*args, **kwargs)
|
||||
|
||||
def render_inner_js_code(self, id_, *args):
|
||||
js = u"$('#%s').data('field_id', '%s');" % (id_, self.field_id)
|
||||
js += super(AutoHeavySelect2Mixin, self).render_inner_js_code(id_, *args)
|
||||
|
|
|
|||
Loading…
Reference in a new issue