Files
music-storage/music_storage/music/models.py

38 lines
975 B
Python

from django.db import models
from core.models import BaseModel
import os
import uuid
def album_cover_upload_to(instance, filename):
ext = filename.split(".")[-1]
return os.path.join("album_covers", f"{uuid.uuid4()}.{ext}")
class Artist(BaseModel):
name = models.CharField(max_length=200)
def __str__(self):
return f"{self.name}"
class Album(BaseModel):
artist = models.ForeignKey(Artist, on_delete=models.CASCADE, related_name="albums")
name = models.CharField(max_length=200)
cover_image = models.ImageField(
upload_to=album_cover_upload_to, null=True, blank=True
)
def __str__(self):
return f"{self.artist} - {self.name}"
class Track(BaseModel):
title = models.CharField(max_length=200)
album = models.ForeignKey(Album, on_delete=models.CASCADE, related_name="tracks")
file = models.FileField(upload_to="music/")
def __str__(self):
return f"{self.album.artist} - {self.title}"