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)
#1 楼
免责声明:我不是Django专家。这是我的想法,它的价值是什么。公用字段:
user
,created
和nodes
几乎出现在每个班级。我会考虑使用这三个字段创建一个抽象基类。字段名称:为了便于阅读,我将
Artist.artist
更改为Artist.name
,将*.user
更改为*.creator
。文件扩展名:偶尔有更多少于3个字符(例如:
.jpeg
,.java
)。索引:我至少要将
db_index=True
添加到name
字段中。 。这称为冗余,通常是一件坏事。主键,唯一的约束:您可能不希望两个具有相同
MusicSheet.song
的Song.artist
,所以该字段是主键(MusicSheet.artist
)的不错选择。我不确定FileType
应该建模什么,但是我猜想它的file_type
也是主键的理想选择。为primary_key=True
添加Level
,或添加带有国家/地区代码的主键。DateTime:我认为不需要此类。只需将出现的所有
level
替换为unique=True
。#2 楼
我想到了一些事情:丢失DateTime模型,而是直接将DateTimeField添加到需要它的模型中。除了为最简单的操作确保额外的数据库联接外,ForeignKey不会给您带来任何好处。
您可能只是这样做,因此可以将此代码粘贴到一块,但如果没有,则:您应该将管理代码放在文件中在您的应用程序内部称为admin.py。这样,您可以使用admin自动发现功能
在DateAdmin中,您可以覆盖response_add方法来设置用户/创建者字段(据我所知)。通过覆盖save_model方法,可以轻松完成此操作。
#3 楼
您可以将它们集成到模型中,而不是最后的Admin类列表(很长),因此:
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()
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
admin.site.register(MusicSheet, MusicSheetAdmin)
可以简化为(并消除)为:
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()
class Admin:
list_display = ["version", "song", "artist", "file_type", "level", "user" , "created", "text", "notes"]
search_fields = ["version"]
确保
class Admin
在主类内。如果您确实要它们是分开的,管理类和注册属于一个单独的文件
admin.py
。 (您必须导入模型from myapp.models import *
)您的某些
ForeignKey
都具有blank=True
和null=True
。这通常不是一个好主意。是否要允许它们为空(无值)或空白(空值)?查看您的代码,我会说您想要null=True
,但我不确定。尝试一个,然后再尝试另一个,然后看看哪个可以满足您的要求。我非常怀疑你们俩都需要。通常,我只将blank=True
用于CharField
,因此您可以使用空字符串而不是空的数据库字段。我无法弄清楚
DateAdmin
的含义。如上面建议的那样,当您切换到DateTimeField时,您可能不需要它,但我想在拨打电话之前先了解其预期的用途。
评论
我建议您突出显示您要反馈的地方。除此之外,从长远来看,对导入和(也许)字段名称进行排序将使您的生活更轻松。@TryPyPY-通常围绕模型本身,类的结构,如何使它更有效?越来越好?如何将这段代码从newb转到pro