Take the 2-minute tour ×

If I have two models in Django application like this:

class Author(models.Model):
    name = models.CharField(max_length=100)

class Book(models.Model):
    author = models.ForeignKey(Author)
    title = models.CharField(max_length=100)

How can I create a single form that allows you add both an Author and a Book simultaneously. If the author exists in the system, I could simply display the book Form and link that to the author but it is very often that I need to allow my users to create the book and the author simultaneously.

How can I do this?


share|improve this question
You could give the user the option to select an author or create a new one then use Ajax calls to create a new author and modify the list to include the newly created one. If that sounds interesting then I can give more information. –  ed. Sep 12 '11 at 11:25
I've thought of similar implementations, Ed, but I'd like to keep it as Djangoic as possible. With the AJAX method, I don't think I'll be able to use the Django's Generic CRUD Views. –  Mridang Agarwalla Sep 12 '11 at 11:32

2 Answers 2

You can write a custom form, which will check if the author exists in the system use existing, if no, create new with provided name.

class CustomForm(forms.ModelForm):
    author = forms.CharField()

    def save(self, commit=True):
       author, created = Author.objects.get_or_create(name=self.cleaned_data['author'])

       instance = super(CustomForm,self).save(commit=commit)
       instance.author = author
       if commit:
       return instance

    class Meta:

Not sure this code is working, but I suppose it can explain my idea.

share|improve this answer
add meta class with model=Book –  szaman Sep 12 '11 at 14:00
Thank you. Done! –  Dmitry Demidenko Sep 12 '11 at 19:21
Hi Dmitry. This solution works fine for my sample case where there's just one field. I jsut used that as an example. When my Author model has a lot of fields, I would have to write each of the fields in the ModelForm. I was looking for a way to directly create a ModelForm based on two forms with needing to redeclare all the fields. Thx. –  Mridang Agarwalla Sep 14 '11 at 13:20

You can create a view that handles multiple forms - see http://collingrady.wordpress.com/2008/02/18/editing-multiple-objects-in-django-with-newforms/ for an excellent example.

You'd have to ensure that the rendering of the form objects are done in the template with only one tag and one submit button.

share|improve this answer

Your Answer


