-# BEGIN HACK - This will not be required after http://code.djangoproject.com/ticket/14082 has been resolved
-
-class EntityFormBase(ModelForm):
- pass
-
-_old_metaclass_new = ModelFormMetaclass.__new__
-
-def _new_metaclass_new(cls, name, bases, attrs):
- formfield_callback = attrs.get('formfield_callback', lambda f, **kwargs: f.formfield(**kwargs))
- new_class = _old_metaclass_new(cls, name, bases, attrs)
- opts = new_class._meta
- if issubclass(new_class, EntityFormBase) and opts.model:
- # "override" proxy fields with declared fields by excluding them if there's a name conflict.
- exclude = (list(opts.exclude or []) + new_class.declared_fields.keys()) or None
- proxy_fields = proxy_fields_for_entity_model(opts.model, opts.fields, exclude, opts.widgets, formfield_callback) # don't pass in formfield_callback
+class EntityFormMetaclass(ModelFormMetaclass):
+ def __new__(cls, name, bases, attrs):
+ try:
+ parents = [b for b in bases if issubclass(b, EntityForm)]
+ except NameError:
+ # We are defining EntityForm itself
+ parents = None
+ sup = super(EntityFormMetaclass, cls)
+
+ if not parents:
+ # Then there's no business trying to use proxy fields.
+ return sup.__new__(cls, name, bases, attrs)
+
+ # Preemptively make a meta so we can screen out any proxy fields.
+ # After http://code.djangoproject.com/ticket/14082 has been resolved,
+ # perhaps switch to setting proxy fields as "declared"?
+ _opts = ModelFormOptions(attrs.get('Meta', None))
+
+ # Make another copy of opts to spoof the proxy fields not being there.
+ opts = ModelFormOptions(attrs.get('Meta', None))
+ if opts.model:
+ formfield_callback = attrs.get('formfield_callback', None)
+ proxy_fields = proxy_fields_for_entity_model(opts.model, opts.fields, opts.exclude, opts.widgets, formfield_callback)
+ else:
+ proxy_fields = {}
+
+ # Screen out all proxy fields from the meta
+ opts.fields = list(opts.fields or [])
+ opts.exclude = list(opts.exclude or [])
+
+ for field in proxy_fields.keys():
+ try:
+ opts.fields.remove(field)
+ except ValueError:
+ pass
+ try:
+ opts.exclude.remove(field)
+ except ValueError:
+ pass
+ opts.fields = opts.fields or None
+ opts.exclude = opts.exclude or None
+
+ # Put in the new Meta.
+ attrs['Meta'] = opts
+
+ new_class = sup.__new__(cls, name, bases, attrs)
+
+ intersections = set(new_class.declared_fields.keys()) & set(proxy_fields.keys())
+ for key in intersections:
+ proxy_fields.pop(key)
+