初学者Django:构建模型时出错

时间:2011-01-23 23:43:26

标签: django django-models

我想知道是否有人可以了解我的代码错误:

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)
  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 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()


class MusicSheetAdmin(admin.ModelAdmin):
  list_display = ["version", "song", "artist", "file_type", "level", "user", "created", "text", "notes"]
  search_fields = ["song"]

class MusicSheetInline(admin.TabularInline):
  model = MusicSheet

class DateAdmin(admin.ModelAdmin):
  list_display = ["datetime"]
  inlines = [MusicSheet]

  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"))

admin.site.register(MusicSheet, MusicSheetAdmin)
admin.site.register(DateTime, DateAdmin)

错误(manage.py syncdb):

Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "/Library/Python/2.6/site-packages/Django-1.2.4-py2.6.egg/django/core/management/__init__.py", line 438, in execute_manager
    utility.execute()
  File "/Library/Python/2.6/site-packages/Django-1.2.4-py2.6.egg/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/Library/Python/2.6/site-packages/Django-1.2.4-py2.6.egg/django/core/management/base.py", line 191, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/Library/Python/2.6/site-packages/Django-1.2.4-py2.6.egg/django/core/management/base.py", line 219, in execute
    self.validate()
  File "/Library/Python/2.6/site-packages/Django-1.2.4-py2.6.egg/django/core/management/base.py", line 249, in validate
    num_errors = get_validation_errors(s, app)
  File "/Library/Python/2.6/site-packages/Django-1.2.4-py2.6.egg/django/core/management/validation.py", line 35, in get_validation_errors
    for (app_name, error) in get_app_errors().items():
  File "/Library/Python/2.6/site-packages/Django-1.2.4-py2.6.egg/django/db/models/loading.py", line 146, in get_app_errors
    self._populate()
  File "/Library/Python/2.6/site-packages/Django-1.2.4-py2.6.egg/django/db/models/loading.py", line 61, in _populate
    self.load_app(app_name, True)
  File "/Library/Python/2.6/site-packages/Django-1.2.4-py2.6.egg/django/db/models/loading.py", line 78, in load_app
    models = import_module('.models', app_name)
  File "/Library/Python/2.6/site-packages/Django-1.2.4-py2.6.egg/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/Users/ahmed/Documents/projects/todolist/musicsheet/models.py", line 122, in <module>
    admin.site.register(DateTime, DateAdmin)
  File "/Library/Python/2.6/site-packages/Django-1.2.4-py2.6.egg/django/contrib/admin/sites.py", line 90, in register
    validate(admin_class, model)
  File "/Library/Python/2.6/site-packages/Django-1.2.4-py2.6.egg/django/contrib/admin/validation.py", line 151, in validate
    "from BaseModelAdmin." % (cls.__name__, idx))
django.core.exceptions.ImproperlyConfigured: 'DateAdmin.inlines[0]' does not inherit from BaseModelAdmin.

1 个答案:

答案 0 :(得分:14)

class DateAdmin(admin.ModelAdmin):
  list_display = ["datetime"]
  inlines = [MusicSheet]

应该是

class DateAdmin(admin.ModelAdmin):
  list_display = ["datetime"]
  inlines = [MusicSheetInline]