HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythondjangoModerate

Music info model

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
musicinfomodel

Problem

I'm learning Django as I go. I know this model is missing user authentication, registration, comments/comment threading, and voting. But this is my starting code for my model. What are some of the things I can improve on, modify, rewrite, etc?

```
from django.contrib.auth.models import User
from django.db import models
from django.contrib import admin
from django.template.defaultfilters import escape
from django.utils.translation import ugettext as _
from django.utils.encoding import force_unicode
from django.http import HttpResponse, HttpResponseRedirect
from django.core.urlresolvers import reverse

class DateTime(models.Model):
datetime = models.DateTimeField(auto_now_add=True)

def __unicode__(self):
return unicode(self.datetime.strftime("%b %d, %Y, %I:%M %p"))

class Country(models.Model):
country = models.CharField(max_length=50)

def __unicode__(self):
return unicode(self.country)

class Artist(models.Model):
artist = models.CharField(max_length=50)
country = models.ForeignKey(Country, blank=True, null=True)
user = models.ForeignKey(User, blank=True, null=True)
created = models.ForeignKey(DateTime)
notes = models.TextField()

def __unicode__(self):
return artist

class Song(models.Model):
name = models.CharField(max_length=200)
artist = models.ForeignKey(Artist, blank=True, null=True)
# language = models.ForeignKey(Country, blank=True, null=True)
user = models.ForeignKey(User, blank=True, null=True)
created = models.ForeignKey(DateTime)
notes = models.TextField()

def __unicode__(self):
return song

class FileType(models.Model):
file_type = models.CharField(max_length=3)
description = models.TextField()
user = models.ForeignKey(User, blank=True, null=True)
created = models.ForeignKey(DateTime)
notes = models.TextField()

def __unicode__(self):
return file_type

class Level(models.Model):
level = models.CharField(max_length=3)
description = models.TextField()
user = models.ForeignKey(User,

Solution

Disclaimer: I'm not a Django guru. Here are my thoughts, for what it's worth.

-
Common fields: user, created, and nodes occur in almost every class. I'd consider creating an abstract base class with these three fields.

-
Field names: for the sake of readability, I'd change Artist.artist to Artist.name, and .user to .creator.

-
File extensions: occasionally have more than 3 chars (e.g.: .jpeg, .java).

-
Indexing: I'd add db_index=True to the name fields, for the very least.

-
Redundancy: since you have MusicSheet.song and Song.artist, you no longer need MusicSheet.artist. It's called redundancy and it's generally a bad thing.

-
Primary keys, unique constraints: you probably don't want two FileTypes with the same file_type, so that field is a good candidate for primary key (primary_key=True). I'm not sure what Level is supposed to model, but my guess is that its level is a good candidate for primary key as well. Add unique=True for Country.country, or add a primary key with country code.

-
DateTime: I don't see a need for this class. Simply replace every occurrence of: created = models.ForeignKey(DateTime) with models.DateTimeField(auto_now_add=True).

Context

StackExchange Code Review Q#187, answer score: 18

Revisions (0)

No revisions yet.