FileField: 'name' returns the path?

5.268 visualizações
Pular para a primeira mensagem não lida

Benjamin Buch

não lida,
16 de ago. de 2008, 08:02:3616/08/2008
para Django users
I have a model with a FileField:

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

Donn

não lida,
16 de ago. de 2008, 08:30:2316/08/2008
para django...@googlegroups.com
On Saturday, 16 August 2008 14:02:36 Benjamin Buch wrote:
> I thought the 'name' attribute should return only the name, without  
> the path. In this case this should be without 'song/'?

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/

Benjamin Buch

não lida,
16 de ago. de 2008, 12:59:1616/08/2008
para django...@googlegroups.com

> Not sure about the docs, but you can always:
> import os
> name = os.path.basename( self.mp3.name )
>
> \d

Thanks! Worked.
Although I'm wondering if there isn't a 'django'-way of doing this...

-benjamin

Marty Alchin

não lida,
16 de ago. de 2008, 14:35:0316/08/2008
para django...@googlegroups.com
On Sat, Aug 16, 2008 at 8:02 AM, Benjamin Buch <benni...@gmx.de> wrote:
> 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'.

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

Marty Alchin

não lida,
16 de ago. de 2008, 14:36:2716/08/2008
para django...@googlegroups.com
On Sat, Aug 16, 2008 at 12:59 PM, Benjamin Buch <benni...@gmx.de> wrote:
> Thanks! Worked.
> Although I'm wondering if there isn't a 'django'-way of doing this...

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

Benjamin Buch

não lida,
16 de ago. de 2008, 15:42:0116/08/2008
para django...@googlegroups.com

> 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

Ticket filed at http://code.djangoproject.com/ticket/8368.

-benjamin

Responder a todos
Responder ao autor
Encaminhar
0 nova mensagem