GE 03 Docs - Sloathking/Foolish-Wizardz GitHub Wiki

GE 03 Main Docs

Author Mattie

Related assignments: GE03

Key takeaways:

  1. Django Models
  2. SQl Relationships In Django
Summery of Notes for this sprint

Summary: Django Models and Database Concepts

Active Record Pattern and Object Relational Mapping (ORM)

In Django, the Active Record Pattern and Object Relational Mapping (ORM) play a crucial role in simplifying database interactions for developers.

Active Record Pattern:

The Active Record Pattern is a design pattern where each database table is represented by a corresponding class in the application code. In Django, these classes are called models. Each model class encapsulates both data and behavior related to a specific table in the database.

python
from django.db import models

class Student(models.Model): name = models.CharField(max_length=100) age = models.IntegerField()

class Portfolio(models.Model): student = models.ForeignKey(Student, on_delete=models.CASCADE) title = models.CharField(max_length=200)

class Project(models.Model): portfolio = models.ForeignKey(Portfolio, on_delete=models.CASCADE) name = models.CharField(max_length=100)

In this example, we have three models: Student, Portfolio, and Project. Each model represents a table in the database, and instances of these classes represent individual records in their respective tables.

Object Relational Mapping (ORM):

Django's ORM provides a powerful abstraction layer over the database, allowing developers to interact with the database using Python objects instead of writing raw SQL queries. The ORM handles tasks such as querying, inserting, updating, and deleting records from the database, making it easier for developers to work with data.

python
# Querying all project objects all_projects = Project.objects.all()

# Getting the first project and printing its portfolio first_project = all_projects.first() print(first_project.portfolio)

In this code snippet, Project.objects.all() queries all project objects from the database, and all_projects.first() retrieves the first project. The ORM translates these operations into appropriate SQL queries behind the scenes.

Routes in Admin App:

Django's admin app provides a convenient interface for managing data in the database. Routes in the admin app define URLs and corresponding views for performing CRUD (Create, Read, Update, Delete) operations on database records.

python
from django.contrib import admin from .models import Student, Portfolio, Project

admin.site.register(Student) admin.site.register(Portfolio) admin.site.register(Project)

By registering the models in the admin app, Django automatically generates admin interfaces for managing data. Developers can customize the admin interface by defining ModelAdmin classes.

python
from django.contrib import admin from .models import Student

class StudentAdmin(admin.ModelAdmin): list_display = ('name', 'age')

admin.site.register(Student, StudentAdmin)

In this example, list_display specifies which fields to display in the list view of the Student model in the admin interface.

Resources:

This summary provides an overview of key concepts related to Django models and database interactions, including the Active Record Pattern, ORM, and routes in the admin app.

⚠️ **GitHub.com Fallback** ⚠️