This simplifies the task of creating a formset for a form that handles multiple instances of a model.
Let's start with my old project:
[mythcat@desk ~]$ cd projects/
[mythcat@desk projects]$ cd django/
[mythcat@desk django]$ source env/bin/activate
Into models.py I add these classes:#create Inline Form with book and author
class Author(models.Model):
author_book = models.CharField(max_length = 100)
def __str__(self):
return self.author_book
class Book(models.Model):
book_name = models.CharField(max_length = 100)
author_book_name = models.ForeignKey(Author,on_delete=models.CASCADE)
def __str__(self):
return self.book_name
(env) [mythcat@desk mysite]$ python3 manage.py makemigrations
Migrations for 'test001':
test001/migrations/0004_author_book.py
- Create model Author
- Create model Book
(env) [mythcat@desk mysite]$ python3 manage.py migrate
Operations to perform:
Apply all migrations: admin, auth, axes, contenttypes, sessions, test001
Running migrations:
Applying test001.0004_author_book... OK
Add the classes to admin.py: from .models import Author, Book
...
admin.site.register(Author)
admin.site.register(Book)
Now you can log in into the admin area and add authors and then add books.Add the source code to views.py:
...
# add to views.py Author and Book
from .models import Author, Book
# Author , Book redirect
from django.shortcuts import redirect
...
# author and book source code
def index_next(request, author_id):
author = Author.objects.get(pk=author_id)
BookFormset = inlineformset_factory(Author,Book, fields=('book_name',))
if request.method == 'POST':
formset = BookFormset(request.POST,instance = author)
if formset.is_valid():
formset.save()
return redirect('index_next',author_id = author_id)
formset = BookFormset(instance = author)
return render(request, 'index_next.html', {'formset': formset})
...
Let's fix the URL for the next step.Add the source code to urls.py:
...
# add index_next to urls.py
from test001.views import index_next
...
urlpatterns = [
...
path('< author_id >',views.index_next, name = 'index_next'),
...
]
Add index_next.html file into the template folder and into this file write HTML5 with a form and one submit button.In the form tag add this:
{{ formset.as_p }}
Run the runserver command:(env) [mythcat@desk mysite]$ python3 manage.py runserver
Use this http://127.0.0.1:8000/1 to see the first on the database shown on the browser.You can customize the output of inline form, see source code:
...
BookFormset = inlineformset_factory(Author,Book, fields=('book_name',), can_delete=False, extra=1)
...
See the full project on my GitHub account at django_chart project repo.