Better fix for registering field_id

This commit is contained in:
AppleGrew (applegrew) 2012-08-23 01:16:40 +05:30
parent 4e240ce415
commit 4479cfbdf7
2 changed files with 8 additions and 9 deletions

View file

@ -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

View file

@ -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)