From f2a1ab52ac2772cc74c299d39fcde795a4be49df Mon Sep 17 00:00:00 2001 From: FeliPython Date: Wed, 17 Apr 2024 22:13:56 -0300 Subject: [PATCH] Add Artist, Album, Song models and update documentation Models for Artist, Album, and Song have been included in the Django application inside the music app. The models were designed to carry out the required relationships in the music domain. Updated documentation to reflect the new additions and provided code examples for better understanding. --- docs/index.md | 44 ++++++++++++++++++++++++++++++++++++--- first_api/music/models.py | 24 ++++++++++++++++++++- 2 files changed, 64 insertions(+), 4 deletions(-) diff --git a/docs/index.md b/docs/index.md index a85882b..3554f9b 100644 --- a/docs/index.md +++ b/docs/index.md @@ -111,9 +111,47 @@ This will create the app structure for us. Something similar to this below: Now the next step is create the models we are going to use in our API to represent the domain models. -- 🎬 Lecture : Fundamentals of Django models. -- 💻 Exercise : Hands-on creation and manipulation of models. -- 💡 Purpose: Understanding data handling in Django. +We are going to create 3 models in the file models.py + +1. Artist +2. Album +3. Song + +Let's start with the artist model. + +```python +class Artist(models.Model): + name = models.CharField(max_length=100) + + def __str__(self): + return self.name +``` + +Don't forget to import the models +```python +from django.db import models +``` +Now the album model +```python +class Album(models.Model): + title = models.CharField(max_length=100) + artist = models.ForeignKey(Artist, on_delete=models.CASCADE) + release_year = models.IntegerField() + + def __str__(self): + return self.title +``` + +the last model will be the song model that will have relationship with artist and album. + +```python +class Song(models.Model): + author = models.CharField(max_length=100) + title = models.CharField(max_length=100) + artist = models.ForeignKey(Artist, on_delete=models.CASCADE) # Artist or band name + album = models.ForeignKey(Album, on_delete=models.CASCADE) # Album the song belongs to + duration = models.IntegerField() # Duration of the song in seconds +``` ## URL Mapping and Views diff --git a/first_api/music/models.py b/first_api/music/models.py index 71a8362..03b7f4c 100644 --- a/first_api/music/models.py +++ b/first_api/music/models.py @@ -1,3 +1,25 @@ from django.db import models -# Create your models here. + +class Artist(models.Model): + name = models.CharField(max_length=100) + + def __str__(self): + return self.name + + +class Album(models.Model): + title = models.CharField(max_length=100) + artist = models.ForeignKey(Artist, on_delete=models.CASCADE) + release_year = models.IntegerField() + + def __str__(self): + return self.title + + +class Song(models.Model): + author = models.CharField(max_length=100) + title = models.CharField(max_length=100) + artist = models.ForeignKey(Artist, on_delete=models.CASCADE) # Artist or band name + album = models.ForeignKey(Album, on_delete=models.CASCADE) # Album the song belongs to + duration = models.IntegerField() # Duration of the song in seconds