I'm getting the following error when I perform:
$ python manage.py schemamigration myapp --initial
It seems to complain about this line:
audio_file = models.FileField(_("Audio file"), upload_to=get_audio_upload_path)
I'm not sure what I'm doing wrong. Please help.
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
Here's my model.py file:
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)
The Python "NameError: name is not defined" occurs when we try to access a variable or function that is not defined or before it is defined. To solve the error, make sure you haven't misspelled the variable's name and access it after it has been declared.
The Python "NameError: function is not defined" occurs when we try to call a function that is not declared or before it is declared. To solve the error, make sure you haven't misspelled the function's name and call it after it has been declared.
In Python, the NameError occurs when you try to use a variable, function, or module that doesn't exist or wasn't used in a valid way. Some of the common mistakes that cause this error are: Using a variable or function name that is yet to be defined.
You miss this:
from django.utils.translation import gettext as _
Read more info from Django i18n docs. It's an idiomatic method in Django
/python
projects.
It should be this now
django.utils.translation import ugettext_lazy as _
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With