I have a queryset. I'm trying to display a feedback form for each of items in the queryset. What's a good way to approach this? Attach the model to a fieldset and then iterate through the forms in a fieldset, displaying the model information? Or loop through both the queryset and fieldsets separately in the template?
Do you mean formset? A Django formset is helpful if you have multiple, identical forms on the same page.
A model formset sounds like it would work based on your description.
Link to the docs:
http://docs.djangoproject.com/en/dev/topics/forms/formsets/
Related
I am trying to update many rows of a model based on user input but I'm having trouble updating them in views.py.
I create the modelForm
class MyForm(forms.ModelForm):
class Meta:
model = MyModel
fields = ["book_title", "book_title_2"]
In my views
def my_view(request):
query = MyModel.objects.filter(foo=foo)
forms = [MyForm(instance=q) for q in query]
context["forms"] = forms
In templates I iterate over forms and input data for some rows. I know normally on form submission I can use form = MyForm(request.POST) and use the is_valid() and save() attributes to save new data to my model. But when I am using a list of forms like above to update many instances, how can I actually save the forms in views.py? When I call MyForm(request.POST) after submitting all of the queried forms at once I get this error 'WSGIRequest' object has no attribute 'FORM' . When I look at the post data I see a list values for book_title and book_title_2. It seems that because I'm submitting more than one form it doesn't work. Is there a workaround for this? Thanks
As posted in the comments by Willem Formsets is exactly what is needed to handle multiple form in views. In my case specifically where I am making multiple forms based on a model and a queryset of that model, modelformset_factory worked
Kindly explain me the what are Django modelform_factory and modelformset_factory, and how do they correlate to the Django models, forms and modelforms?
I'm a beginner to Django and came across these terms. Looked up on the internet but wasn't able to find any good source of information on these.
Thank you.
modelform_factory is a method to create forms in views , without defining form in forms.py file.
There are multiple places, where you have automatically generated forms or formsets, the most visible is admin and Generic Class Views.
Every time you are working with HTML forms do add or modify data - you should use forms.Form. If you are working with the model - there is forms.ModelForm which is build on top of the Form - handling automatically declaration of fields, some validations and instance save. Then on top of this you have modelform_factory - which is generating on-demand ModelForm - so it is kind of shortcut to define default ModelForm for your model.
Similar, modelformset_factory is generating ModelFormSet to be used with one-to-many relations.
For example, I have a Post model:
Class Post(models.Model):
title = models.Charfield(max_length=200)
# other fields
I wonder is there a way to create multiple posts at once in admin. In other words, I need a formset instead of single form on post creation page.
I've heard recently about a django app that exactly does this job. It's called django-bulk-admin and enables bulk add/update in the admin.
Possibly, the best way to do exactly what you want is extend the ModelAdmin class, because it has no formsets on it, except for those used on InlineFormsets.
After that you could customize the admin change_form template, to include your formsets
The quick-and-dirty way to do it using admin is wrap your Post model as an inline formset of another modeladmin and add the extra option to it.
Say I have a single form that has a field type and depending on what type the user inputs, the data is placed into a model/table. How do you do this with django forms?
Thanks!
In Django, a form for more than one model is called a FormSet.
I've created a onetomany relationship between two models. The model
that has the foreignkey I can use their formset without problems, but
now I want to fill this relation through the form of the model that
has the "many", I know I can retrieve it using the *_set, but how
can I create a MultipleChoiceField form element of this data?
Thanks for any help!
Regards,
Thiago
Have a look at the docs for fields which handle relationships. You could use a ModelMultipleChoiceField -- it's essentially a MultipleChoiceField that takes its choices from a queryset.