Lesson 27 - Database Join
LEARN
Complete Your Projects
- Project 1 - Setup development tools
- Project 2 - Build a simple app
- Project 3 - Application Hosting
- Project 4 - TemplateView
- Project 5 - Data Models
- Project 6 - Data Model Views
- Project 7 - User Authentication
- Project 8 - Bootstrap Styling
- Project 9 - Custom Views
- Project 10 - Relational Data
Today
- Mentoring
- Relational Data
- Foreign Key
Real world Engineering
- Skills are needed beyond programming
- Mentoring
- Design reviews
- Each one teach one
- Introverts and extroverts
Introducing a new mentoring program
- Best in the band, get a new band
- Best learning is teaching
- Gives you a chance to teach someone else
- Invitation
Benefits
- Accelerate your learning
- Help someone else to succeed
- Line on the resume
- I will act as a reference
- Boost of one letter grade
Requirements
- Current on assignments
- Meet with someone 1 hour weekly
- Review code and advise
- Teach misunderstandings
Some students need help
- If you invest the time you will succeed
- Mentoring is an extra resource
- Dedicated time and place to review work
- Have the wisdom and humility to use the resources
Relational Data
- Data table point to each other
- Objects live in the tables
- Reduce the duplicate information
- Databases are joined by a query
Foreign Key
- Points to a record in another table
- Saves to ID of the record
- Guarantees that data is consistent
- Data duplication is removed
Relational Example
- Chapters belong to a Book
- Chapter Data Record
- book (points to a book ID)
- order
- title
Chapter Data Model
Create a representation for Chapters in the Book
```python class Chapter(models.Model): book = models.ForeignKey(Book, on_delete=models.CASCADE) order = models.IntegerField() title = models.CharField(max_length=200) markdown = models.TextField() html = models.TextField() document = models.CharField(max_length=200)
# Bad
book = models.CharField(max_length=200)
```
Superhero News
-
Superhero Record
- name
- identity
- description
- strength
- weakness
- image
-
Article Record
- hero (ID of Hero)
- order
- title
- markdown
- html
python
class Article(models.Model):
hero = models.ForeignKey(Hero, on_delete=models.CASCADE)
order = models.IntegerField()
title = models.CharField(max_length=200)
markdown = models.TextField()
html = models.TextField()
Custom Queries in List View
- Find only matching objects
- Pass reference object by ID on URL
- Use get_queryset to filter objects
Django for Beginners - Chapter 15
- Comments on Articles
- Articles app
- URL, Templates, Views
- ForeignKey - Comment, Article
BUILD
Book Builder Demo
- Add Author Data Type
- Chapter point to Book
- Books point to Authors
- Author <-- Book <-- Chapter
- Views support proper queries
- Book builder style
- Tests for Data/Views (Author, Book, Chapter)
Limitation of Week 9
- App support Books and Chapters
- But they are not integrated
- Massive duplication of data
- Chapters do not actually belong to a book
Work Needed
- Show Chapters in Book Details page
- Rework URLs to display chapters
- Rework URLs to edit chapters
Chapters in Book
List the Chapters in the Book
book/views.py
```python
urls.py
path('book/', BookDetailView.as_view(), name='book_detail'),
class BookDetailView(DetailView): template_name = 'book_detail.html' model = Book
def get_context_data(self, **kwargs):
book = Book.objects.get(pk=self.kwargs['pk'])
return dict(object=book, chapters=Chapter.objects.filter(book=book.title))
```
Chapters in Book
Show the Chapters in the Book in detail view
templates/book_detail.html
```html
{{ chapter.book }} | Chapter {{ chapter.order }} - {{ chapter.title }} | Edit | Delete |
```
Project 10 - Superhero News
- Views
- Query for Articles about each Hero in HeroDetailView
- Add the article list to hero detail template
- Data
- Add hero as ForeignKey in Article
- Migrate
- Tests
- Hero Data
- Hero Views
- Article Data
- Article Views
Project 10 - Views
- Superhero Selector Page
- Profile Tab
- News Tab
- Article Views
- Hero Views
Project 10 - Data
- Investigator Data Type
- Investigator <-- Hero <-- Article
- Views support proper queries
- Hero News style
Project 10 - Tests
- Data/Views
- Investigator, Hero, Article