Newwbie trying to get a variable name into a field name

3 views
Skip to first unread message

dave....@gmail.com

unread,
Feb 7, 2008, 4:01:55 AM2/7/08
to Django users
Here is my basic model:

class Type(models.Model):
name = models.CharField()
description = models.TextField(blank=True)
created = models.DateTimeField(auto_now_add)
modified = models.DateTimeField(auto_now)

class Media(models.Model):
name = models.CharField()
description = models.TextField(blank=True)
media_type = models.ForeignKey(Type)
path_to_file = models.FileField(upload_to='/%s/%Y/%m/%d/ %s
media_type')
created = models.DateTimeField(auto_now_add)
modified = models.DateTimeField(auto_now)
slug = models.SlugField(prepopulate_from=("name",))

I know I can't do what I am trying to do in the Media class, but how
can I do this? How can I name that folder from the media_type
fieldname?

Lemme know. Thanks.

Nathaniel Whiteinge

unread,
Feb 7, 2008, 11:57:37 AM2/7/08
to Django users
On Feb 7, 2:01 am, "dave.mer...@gmail.com" <dave.mer...@gmail.com>
wrote:
> I know I can't do what I am trying to do in the Media class, but how
> can I do this? How can I name that folder from the media_type
> fieldname?

You have to override the _save_FIELD_file method in your model. Marty
Alchin has a good write-up [1] of how to do it.

.. [1] http://gulopine.gamemusic.org/2007/nov/07/customizing-filenames-without-patching/

dave....@gmail.com

unread,
Feb 7, 2008, 2:59:55 PM2/7/08
to Django users
Thanks Nathaniel, that seems like it will work.
> .. [1]http://gulopine.gamemusic.org/2007/nov/07/customizing-filenames-witho...
Reply all
Reply to author
Forward
0 new messages