Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a model and related models with Inline formsets

[I have posted this at the Django users | Google Groups also.]

Using the example in the inline formset docs, I am able to edit objects belonging a particular model (using modelforms). I have been trying to follow the same pattern for creating new objects using inline formsets, but have been unable to clear my head enough to bring out a working view for this purpose.

Using the same example as in the above link, how would I go about creating a new instance of an "Author" model together with its related "Book" objects?

like image 207
chefsmart Avatar asked Jul 11 '09 05:07

chefsmart


People also ask

How do you create a model form?

First, create a model that contains fields name and other metadata. It can be used to create a table in database and dynamic HTML form. This file contains a class that inherits ModelForm and mention the model name for which HTML form is created. Write a view function to load the ModelForm from forms.py.

What is model form used for?

Django ModelForm is a class that is used to directly convert a model into a Django form. If you're building a database-driven app, chances are you'll have forms that map closely to Django models. For example, a User Registration model and form would have the same quality and quantity of model fields and form fields.

What is model form?

Model Forms are forms that are connected directly to models, allowing them to populate the form with data. It allows you to create a form from a pre-existing model. You add an inline class called Meta, which provides information connecting the model to the form. An inline class is a class within another class.

What is instance in form Django?

To facilitate this process, Django adds the instance field to the form reference, containing the form data structured as an instance of the underlying model of the model form. The instance field is particularly important when you need or must manipulate the model data prior to attempting a model operation.


1 Answers

First, create a Author model form.

author_form = AuthorModelForm() 

then create a dummy author object:

author = Author() 

Then create a inline formset using the dummy author like so:

formset = BookFormSet(instance=author)  #since author is empty, this formset will just be empty forms 

Send that off to a template. After the data is returned back to the view, you create the Author:

author = AuthorModelForm(request.POST) created_author = author.save()  # in practice make sure it's valid first 

Now hook the inline formset in with the newly created author and then save:

formset = BookFormSet(request.POST, instance=created_author) formset.save()   #again, make sure it's valid first 

edit:

To have no checkboxes on new forms, do this is a template:

{% for form in formset.forms %}     <table>     {% for field in form %}         <tr><th>{{field.label_tag}}</th><td>{{field}}{{field.errors}}</td></tr>     {% endfor %}      {% if form.pk %} {# empty forms do not have a pk #}          <tr><th>Delete?</th><td>{{field.DELETE}}</td></tr>     {% endif %}     </table> {% endfor %} 
like image 53
priestc Avatar answered Oct 11 '22 20:10

priestc