Boilderplate: Make A Many-To-Many Model Connection In Django
Introduction
This boilerplate creates an interface for Books and Authors to demonstarte a many-to-many connection. It assumes you've already gone through the basic site/project setup steps (i.e. Boilerplate: Create A New Django Site/Project , Boilerplate: Add Basic Template And Static File Handling In Django , and Boilerplate: Add Basic User Authencation To A Django Site ). More details are below the code in the Boilerplate Details section.
Boilerplate Steps
Perp Work
-
Make sure you've started the Python venv and are in the directory with: manage.py
TODO: Write up how to make sure you're in the proper vecv
-
Create app modules with
startapp
terminal commands -
Make the templates directories
terminal commands -
Update INSTALLED_APPS in
site_files/settings.py site_files/settings.py - section update -
Add the top level url patterns:
site_files/urls.py - full content replacement
Make The Authors
-
Make the authors model
authors/models.py - full content replacement -
Make the "authors/urls.py" file:
authors/urls.py - new file to create -
Add the admin for the authors page
authors/admin.py - full content replacement -
Add the functions to:
author/views.py authors/views.py - full content replacement -
Add the forms.py file:
authors/forms.py - new file to create -
Create the
index.html templateauthors/templates/authors/index.html - new file to create -
Create the
view.html templateauthors/templates/authors/view.html - new file to create -
Create the
create.html templateauthors/templates/authors/create.html - new file to create -
Create the
edit.html templateauthors/templates/authors/edit.html - new file to create -
Create the
delete.html templateauthors/templates/authors/delete.html - new file to create
Make The Books
-
Make the books and book_authors models
books/models.py - full content replacement -
Make the "books/urls.py" file:
books/urls.py - new file to create -
Add the admin for the books page
books/admin.py - full content replacement -
Add the functions to:
book/views.py books/views.py - full content replacement -
Add the forms.py file:
books/forms.py - new file to create -
Create the
index.html templatebooks/templates/books/index.html - new file to create -
Create the
view.html templatebooks/templates/books/view.html - new file to create -
Create the
create.html templatebooks/templates/books/create.html - new file to create -
Create the
edit.html templatebooks/templates/books/edit.html - new file to create -
Create the
delete.html templatebooks/templates/books/delete.html - new file to create
Finishing Up
-
Run the migrations
terminal commands -
Start the server:
terminal commands -
Visit the page:
link
Boilerpage Details
-
This boilerplate demonstrates a basic many-to-many connection between database items.
-
The example uses Books and Authors.
-
You can view books and authors without being logged in.
-
You have to be logged in in order to add/edit/delete books and authors.
-
Each Book can have zero or more Authors.
-
Each Author can have zero or more Books.
-
You can delete a Book at any point.
-
You can only delete Authors when they don't have any books associated with them.
-
Author names have to have a first name, but the last name is not required.