jrief / django-angular

Let AngularJS play well with Django
http://django-angular.awesto.com/
MIT License
1.23k stars 293 forks source link

django-angular and formsets #212

Closed lmores closed 7 years ago

lmores commented 8 years ago

I've been using django formsets and unfortunately they don't play well with django-angular.

First of all I've had to deal with default naming of forms: form-0, from-1, ... which are illegal variable names in javascript (which led to runtime error "unexpected token" in angularjs). I solved this issue by overwriting django BaseModelFormSet:

class NgModelFormSet(DjngBaseModelFormSet):
    def __init__(self, *args, **kwargs):
        # Makes sure that child forms render errors correctly...
        kwargs.update(error_class=TupleErrorList)
        super(NgModelFormSet, self).__init__(*args, **kwargs)

    def add_prefix(self, index):
        # Prevents default separator '-' from raising javascript runtime error
        return '%s_%s' % (self.prefix, index)

and passing it to the factory:

MyFormset = modelformset_factory(MyModel, form=MyForm, formset=NgModelFormSet)

and that seems to solve the issue.

But now I'm facing problems with {{ formset.management_form}} which raises the error ManagementForm data is missing or has been tampered with on template rendering. That's because it's instance property self.errors contains this garbage:

<ul class="errorlist"><li>TOTAL_FORMS<ul class="error list”><li>This field is required.</li></ul></li><li>INITIAL_FORMS<ul class="errorlist"><li>This field is required.</li></ul></li></ul>

and I'm not familiar enough with django-angular to solve it. Is anyone planning to integrate formsets in this still very useful project?

P.S.: I'm new to django-angular and GitHub.

adrienbrunet commented 8 years ago

Can you provide the template of the forms you're using? I feel like you forgot one part. See http://stackoverflow.com/questions/18022792/django-managementform-data-is-missing-or-has-been-tampered-with-when-saving-mo Tell us if it's relevant. Right now, formset is not yet supported correctly but If you feel you can find a stable solution, we'd like to hear from you so that we can integrate this to the library. Cheers

lmores commented 8 years ago

My bad, the error was due to a wrong kwarg I was passing to MyFormSet in the view (and, yes, I printed the management_form fields in my template).

Anyhow the NgModelFormSet hack I pasted in my first comment seems to be definitely mandatory.

I'll leave the issue open and keep doing some more experiments before reporting my results and then I'll close it. Is it ok?

adrienbrunet commented 8 years ago

Yes sure. If you get a working example with formsets, would you have the time and the kindness to submit a pull request with your changes? It would a good plus to this library. It's been a while people ask for formset support... We did not manage to find the time to do it yet ! Cheers

lmores commented 8 years ago

In the end I changed my mind and I decided to render my forms completely client side using pure angular. Therefore I didn't further investigate possible implementations for this package. Anyway all the above remains unchanged, imho.

Sirion

P.S.: should I close this issue?

adrienbrunet commented 8 years ago

let it openned. At some point, we should reach a point where we have a complete working example with formsets... :pray: