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, blank=True, null=True)
created = models.ForeignKey(DateTime)
notes = models.TextField()
def __unicode__(self):
return level
class MusicSheet(models.Model):
version = models.CharField(max_length=2)
song = models.ForeignKey(Song, blank=True, null=True)
artist = models.ForeignKey(Artist, blank=True, null=True)
file_type = models.ForeignKey(FileType, blank=True, null=True)
level = models.ForeignKey(Level, blank=True, null=True)
user = models.ForeignKey(User, blank=True, null=True)
created = models.ForeignKey(DateTime)
text = models.TextField()
notes = models.TextField()
#include votes
#include comments
#include registration code
########################################################################################################################
######################################## ADMIN STUFF ###################################################################
########################################################################################################################
class MusicSheetAdmin(admin.ModelAdmin):
list_display = ["version", "song", "artist", "file_type", "level", "user" , "created", "text", "notes"]
search_fields = ["version"]
class MusicSheetInline(admin.TabularInline):
model = MusicSheet
class DateAdmin(admin.ModelAdmin):
list_display = ["datetime"]
inlines = [MusicSheetInline]
def response_add(self, request, obj, post_url_continue='../%s/'):
""" Determines the HttpResponse for the add_view stage. """
opts = obj._meta
pk_value = obj._get_pk_val()
msg = "Song(s) were added successfully."
# Here, we distinguish between different save types by checking for
# the presence of keys in request.POST.
if request.POST.has_key("_continue"):
self.message_user(request, msg + ' ' + _("You may edit it again below."))
if request.POST.has_key("_popup"):
post_url_continue += "?_popup=1"
return HttpResponseRedirect(post_url_continue % pk_value)
if request.POST.has_key("_popup"):
return HttpResponse(
'<script type="text/javascript">opener.dismissAddAnotherPopup(window, "%s", "%s");'
'</script>' % (escape(pk_value), escape(obj)))
elif request.POST.has_key("_addanother"):
self.message_user(request, msg + ' ' + (_("You may add another %s below.") %
force_unicode(opts.verbose_name)))
return HttpResponseRedirect(request.path)
else:
self.message_user(request, msg)
for music_sheet in MusicSheet.objects.filter(created=obj):
if not music_sheet.user:
music_sheet.user = request.user
music_sheet.save()
return HttpResponseRedirect(reverse("admin:musicsheet_musicsheet_changelist"))
class CountryAdmin(admin.ModelAdmin):
list_display = ["country"]
search_fields = ["country"]
class CountryInline(admin.TabularInline):
model = MusicSheet
class SongAdmin(admin.ModelAdmin):
list_display = ["name", "artist", "user", "created", "notes"]
search_fields = ["name", "artist"]
class SongInline(admin.TabularInline):
model = MusicSheet
class ArtistAdmin(admin.ModelAdmin):
list_display = ["artist", "country", "user", "created", "notes"]
search_fields = ["artist", "country"]
class ArtistInline(admin.TabularInline):
model = MusicSheet
class FileTypeAdmin(admin.ModelAdmin):
list_display = ["file_type", "description", "user", "created", "notes"]
search_fields = ["file_type"]
class FileTypeInline(admin.StackedInline):
model = MusicSheet
class LevelAdmin(admin.ModelAdmin):
list_display = ["level", "description", "user", "created", "notes"]
search_fields = ["level"]
class LevelInline(admin.StackedInline):
model = MusicSheet
admin.site.register(MusicSheet, MusicSheetAdmin)
admin.site.register(Country, CountryAdmin)
admin.site.register(Song, SongAdmin)
admin.site.register(Artist, ArtistAdmin)
admin.site.register(FileType, FileTypeAdmin)
admin.site.register(Level, LevelAdmin)
admin.site.register(DateTime, DateAdmin)