class Recording(models.Model):
song = models.ForeignKey(Song)
mp3 = models.FileField(upload_to='songs')
description = models.TextField(blank=True)
date_recorded = models.DateField()
represents_song = models.BooleanField()
def __unicode__(self):
return self.mp3.name
If I try to access the files' 'name' attribute (__unicode__), it
returns the path.
In the admin interface, every recording is represented by the pattern
'songs/somerecording.mp3'.
According to the documentation (http://www.djangoproject.com/documentation/files/#using-files-in-models
) I thought the 'name' attribute should return only the name, without
the path. In this case this should be without 'song/'?
-benjamin
Not sure about the docs, but you can always:
import os
name = os.path.basename( self.mp3.name )
\d
--
If we believe absurdities, we shall commit atrocities.
-- Voltaire
Fonty Python and other dev news at:
http://otherwiseingle.blogspot.com/
Thanks! Worked.
Although I'm wondering if there isn't a 'django'-way of doing this...
-benjamin
That's the way Python's own files work, so that's the way Django's
File object works as well.
> According to the documentation (http://www.djangoproject.com/documentation/files/#using-files-in-models
> ) I thought the 'name' attribute should return only the name, without
> the path. In this case this should be without 'song/'?
That does seem to be a bug in the documentation. Would you mind filing
a ticket for it so we can keep track of it?
-Gul
As with many things in Django, we take our cues here from Python,
which just contains the relative path in .name, requiring
os.path.basename() to get just the name itself. It'd be possible to
include a .basename attribute on Django's File object, but I'm not
convinced it's be useful, just to separate ourselves more from Python.
-Gul
Ticket filed at http://code.djangoproject.com/ticket/8368.
-benjamin