Closed gbozee closed 8 years ago
Can you please try to apply this patch:
diff --git a/djangular/forms/angular_base.py b/djangular/forms/angular_base.py
index af2dae5..30df4e4 100644
--- a/djangular/forms/angular_base.py
+++ b/djangular/forms/angular_base.py
@@ -160,10 +160,10 @@ class NgFormBaseMixin(object):
field_error_css_classes = 'djng-field-errors'
field_mixins_module = field_mixins_fallback_module = 'djangular.forms.field_mixins'
- def __new__(cls, **kwargs):
+ def __new__(cls, *args, **kwargs):
field_mixins_module = import_module(cls.field_mixins_module)
field_mixins_fallback_module = import_module(cls.field_mixins_fallback_module)
- new_cls = super(NgFormBaseMixin, cls).__new__(cls)
+ new_cls = super(NgFormBaseMixin, cls).__new__(cls, *args, **kwargs)
# add additional methods to django.form.fields at runtime
for field in new_cls.base_fields.values():
FieldMixinName = field.__class__.__name__ + 'Mixin'
yes, I'd prefer to call new as __new__(cls, name, bases, attrs)
but some weird reasons this does not work.
Thanks for the above patch. Worked like a charm
This is not fixed yet.
What is the exact status of this issue? The above patch seems to work for me as well but what are you planning to code?
__new__(cls, name, bases, attrs)
is the aim?
Any help needed? If yes, do you have a branch or some tests to show what should actually be done? (I need that part to be fixed...)
To use __new__(cls, name, bases, attrs)
, we shoud useclass NgFormBaseMixin(type):``instead of
class NgFormBaseMixin(object):` right? I've never encountered such modification before so I'm not sure what is the next step but I guess then, this Mixin should actually be called in another way (metaclass). As it's the first time I'm using that kind of things, I may be plain wrong. If you have some guidances... I'm listening!
@adrienbrunet sure! Thank for reporting.
Inheritance from the mixin should be done using six.with_metaclass(...)
.
This will be fixed in 0.7.10. I hope to find some time soon.
I tried:
class NgFormBaseMeta(type):
field_mixins_module = field_mixins_fallback_module = 'djangular.forms.field_mixins'
def __new__(cls, name, bases, attrs):
field_mixins_module = import_module(cls.field_mixins_module)
field_mixins_fallback_module = import_module(cls.field_mixins_fallback_module)
new_cls = super(NgFormBaseMeta, cls).__new__(cls, name, bases, attrs)
# add additional methods to django.form.fields at runtime
for field in new_cls.base_fields.values():
FieldMixinName = field.__class__.__name__ + 'Mixin'
try:
FieldMixin = getattr(field_mixins_module, FieldMixinName)
except AttributeError:
try:
FieldMixin = getattr(field_mixins_fallback_module, FieldMixinName)
except AttributeError:
FieldMixin = field_mixins_fallback_module.DefaultFieldMixin
field.__class__ = type(field.__class__.__name__, (field.__class__, FieldMixin), {})
return new_cls
@add_metaclass(NgFormBaseMeta)
class NgFormBaseMixin(object):
form_error_css_classes = 'djng-form-errors'
field_error_css_classes = 'djng-field-errors'
def __init__(self, data=None, *args, **kwargs):
try:
form_name = self.form_name
.....
But at this point, in __new__
, the object does not have base_fields
yet.
Do you think it's that important to stick to name, base, attrs
?
Is it to stick to django FormMixinBase?
I guess I need to wait for version 0.7.10 :smile:
By the way, my company (luna technology) is actually using django-angular but we are still under heavy development so I don't have any working link to provide. I'll tell you as soon as I get one.
This weekend a tried out a few things. The problem is, that I have to add a mixin class, built on the fly, to all of our form fields. This is, because each form field requires an additional method get_potential_errors
to render potential errors activated by AngularJS's form validation code.
Now, if I do it in __init__
, its too late. If I do it in a __new__
of a meta-class, the member base_fields
is not available yet. I have to experiment a little to find out the best hook for patching the form fields as required by class NgFormValidationMixin
.
@gbozee @adrienbrunet Now I managed to find a clean solution.
The big problem here is that django.forms.Form
and django.forms.ModelForm
required a metaclass themselves. Since you can't mix metaclasses in a friendly manner, I had to inherit my own metaclass BaseFieldsModifierMetaclass
from the ones, given by Django: DeclarativeFieldsMetaclass
and ModelFormMetaclass
.
Now however we can't just add some mixins to our own Form
s or ModelForm
s. Therefore instead you now have to derive your classes from djangular.forms.NgForm
or djangular.forms.NgModelForm
respectively.
Please test with the latest revision, so that I can release version 0.7.10.
Just tested the above patch. Working as expected.
:+1: Tested with your last commits. I have to rewrite a lot of my forms now but it's for the best.. Thanks for your work!
I keep on getting the following error in my test suit when passing an instance to my Form class.
I am initializing my form like this
but any time i run any test using the above method, I get the following error.
I traced this error down to the base class
NgFormBaseMixin
Any help would be greatly appreciated.