Monday, January 23, 2017

Re: Multiple forms in the same template

Hi!
And thanks for the answer.

 

This stopped working in Django 1.8 :

"Class-based views that use ModelFormMixin will raise an ImproperlyConfigured exception when both the fields and form_class attributes are specified. Previously, fields was silently ignored."


But there is no form_class defined. There is a form_classes dictionary, but this is not a form_class attribute. Form this point of view it should work.
But it seems that it is not able to get the fields of those ModelForms (fields are defined for both ModelForms), if this dictionary with both forms is passed to the get_context_data.
Is it allowed to pass such a dictionary, or which prerequisites have to be fulfilled?


In older versions, omitting both fields and exclude resulted in a form with all the model's fields. Doing this now raises an ImproperlyConfigured exception.
This was really the case here, but I already removed all include and exculde and exchanged them with a corresponding fields attribute.

I guess I first need to fully understand the Django ModelForm and how a template can handle two independent forms. Maybe I also have to change the concept?
data from different models have to be displayed on one template and for that 2 ModelForms were generated and added to a main view, but this does notwork in 1.9 till now.

Thanks for any hints.
schaf


Am Montag, 23. Januar 2017 16:46:15 UTC+1 schrieb Melvyn Sopacua:

On Monday 23 January 2017 06:41:53 scha...@gmail.com wrote:

 

> This worked well like that until Django 1.8, but now with Django 1.9

 

This stopped working in Django 1.8 :

"Class-based views that use ModelFormMixin will raise an ImproperlyConfigured exception when both the fields and form_class attributes are specified. Previously, fields was silently ignored."

 

And burried in the docs, a different case for the same thing:

In older versions, omitting both fields and exclude resulted in a form with all the model's fields. Doing this now raises an ImproperlyConfigured exception.

 

For someone who didn't follow the discussion on the topic, it sure is hard to find in release notes.

 

> the

> following error occurs:

> > Using ModelFormMixin (base class of MFormsView) without the 'fields'

> > attribute is prohibited.

> Now my question:

> 1.) Why do I get this message, as the fields are defined in both

> ModelForms?

 

The model form is missing a 'fields' attribute. Example:

class ComponentStockAllocationAssign(StockMixin, generic.CreateView):
model = models.ComponentLocationStock
fields = ['component', 'location', 'stock']
success_url = reverse_lazy("component_allocation_list")

 

> 2.) What is the best way to include 2 different forms

> into the same template? Do you have an example?

 

That's a much bigger question than it seems. What's the relation between the forms?

If they're unrelated, implement get_context_data() to assign them, and form_valid() to process them.

Also, bookmark this: https://ccbv.co.uk/

--

Melvyn Sopacua

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/8622a8ab-f597-4822-b7bf-080fc2acf3f4%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment