20

执行时出现以下错误:

$ python manage.py schemamigration myapp --initial

它似乎抱怨这条线:

audio_file = models.FileField(_("Audio file"), upload_to=get_audio_upload_path)

我不确定我做错了什么。请帮忙。

Traceback (most recent call last):
 File "/Users/home/Desktop/Web Development/Aptana Studio 3.0/musicproject/src/manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
utility.execute()
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/core/management/__init__.py", line 382, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/core/management/base.py", line 196, in run_from_argv
self.execute(*args, **options.__dict__)
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/core/management/base.py", line 231, in execute
self.validate()
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/core/management/base.py", line 266, in validate
num_errors = get_validation_errors(s, app)
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/core/management/validation.py", line 30, in get_validation_errors
for (app_name, error) in get_app_errors().items():
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/db/models/loading.py", line 158, in get_app_errors
self._populate()
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/db/models/loading.py", line 64, in _populate
self.load_app(app_name, True)
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/db/models/loading.py", line 88, in load_app
models = import_module('.models', app_name)
File "/Users/home/virtualenv/venv/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
__import__(name)
File "/Users/home/Desktop/Web Development/Aptana Studio 3.0/musicproject/src/myapp/models.py", line 39, in <module>
class AudioTrack(models.Model):
File "/Users/home/Desktop/Web Development/Aptana Studio 3.0/musicproject/src/myapp/models.py", line 51, in AudioTrack
audio_file = models.FileField(_("Audio file"), upload_to=get_audio_upload_path)
 NameError: name '_' is not defined

这是我的 model.py 文件:

import os
import mimetypes

from django.conf import settings
from django.contrib.auth.models import User

from django.db import models

GENRE_CHOICES =  (
             ('R', 'Rock'),
             ('J/B', 'Jazz/Blues'),
             ('B', 'Blues'),
             ('R&B' 'R&B'),
             ('J', 'Jazz'),
             ('P', 'Pop'), 
             ('H', 'Hip-Hop'),    
             )


def get_upload_path(dirname, obj, filename):
   return os.path.join("audiotracks", dirname, obj.user.username, filename)

def get_audio_upload_path(obj, filename):
   return get_upload_path("audio_files", obj, filename)

class Genre(models.Model):
   genre_choices = models.CharField(max_length=1, choices=GENRE_CHOICES)
   slug = models.SlugField(max_length = 40, unique = True) #so as to have a dedicated page for each category 
   description = models.TextField()

def __unicode__(self):
       return self.title

def get_absolute_url(self):
    return "/genres/%s/" % self.slug

class AudioTrack(models.Model):
   class Meta:
      abstract = True

   user = models.ForeignKey(User, 
      related_name = "tracks",
      blank = True,
      null = True
  )

added_on = models.DateTimeField(auto_now_add=True, null = True)
updated_on = models.DateTimeField(auto_now=True, null = True)
audio_file = models.FileField(_("Audio file"), upload_to=get_audio_upload_path)
title = models.CharField(_("Title"), max_length="200", null=True)
description = models.TextField(_("Description"), null=True, blank=True)
slug = models.SlugField(max_length = 40, unique = True) #so as to have a dedicated page for each category 
genres = models.ManyToManyField(Genre)

def __unicode__(self):
    return "Track '%s' uploaded by '%s'" % (self.title, self.user.username)

@property
def mimetype(self):
    if not hasattr(self, '_mimetype'):
        self._mimetype = mimetypes.guess_type(self.audio_file.path)[0]
    return self._mimetype

@property
def filetype(self):
    if '/' in self.mimetype:
        type_names = {'mpeg': 'MP3', 'ogg': 'Ogg Vorbis', 'wave':'wav', 'FLAC':'FLA'}
        filetype = self.mimetype.split('/')[1]
        return type_names.get(filetype, filetype)
    else:
        return self.mimetype

@models.permalink
def get_absolute_url(self):
    # ('tracks.views.track_detail', [self.user.username, self.slug])
    return "/%s/%s/" %(self.genre, self.slug)

if hasattr(settings, 'AUDIOTRACKS_MODEL'):
   app_name, model_name = settings.AUDIOTRACKS_MODEL.split('.')
   Track = models.get_model(app_name, model_name)
else:
   class Track(AudioTrack):
      pass

class Genre_Track(models.Model): #links genre to tracks
   audio_track = models.ForeignKey(AudioTrack)
   genre = models.ForeignKey(Genre)
4

2 回答 2

67

你错过了这个:

from django.utils.translation import gettext as _

从Django i18n 文档中阅读更多信息。Django这是/python项目中的惯用方法。

于 2012-10-21T07:40:39.440 回答
3

现在应该是这个 django.utils.translation import ugettext_lazy as _

于 2020-09-14T06:01:00.843 回答