I'm in the middle of a very strange issue here.
I have a FileField with a default value in a model declared as follow:
class MyModel(models.Model):
name = models.CharField(max_length=32)
audio_file = models.FileField(upload_to='user_menus/', default='%suser_menus/default.mp3' % settings.MEDIA_ROOT, blank=True, null=False)
Now, when I do the following
>>> a = MyModel(name='Foo')
>>> a.save()
>>> a.audio_file.path
'/full/path/to/file'
>>> a.audio_file.url
'/full/path/to/file' # again
I have my MEDIA_ROOT and MEDIA_URL configured as follows
MEDIA_ROOT = os.path.join(os.path.dirname(__file__), 'static/')
MEDIA_URL = '/media/'
Am i missing something? any advice?
Thank you in advance.
You need to specify in the default value of the field the actual value (string) you want to save in the database, not the full path. That's why the .url
is showing up that way. For your case should be like this:
audio_file = models.FileField(upload_to='user_menus/', default='%suser_menus/default.mp3' % settings.MEDIA_URL, blank=True, null=False)
Notice that I just think you'll having this problem when the default
is inserted in the database.
Hope this helps!