You can not do what you are trying to do, the way you are doing it ;-)
The problem is the 'test_name' does not refer to the data on the
instance. It just evaluates to the local variable.
That means you are constructing that upload_to string at import time.
It is the equivalent of doing:
foo = models.CharField(maxlength=200)
class TestDetail(models.Model):
test_name = foo
python_file = models.File("Python File", upload_to = 'tests/' +
str(foo) + '/')
In other words, you can not just have the FileField use data from the
particular TestDetail instance.
To get around this, you need to create a new type of FileField. An
example of doing exactly this can be found here:
https://pycon.coderanger.net/browser/django/trunk/pycon/schedule/models.py#L256
NOTE: this is not the best example, and it hard codes using the id of
a relation instead of being able to supply a field name, and it also
has the limitation of being old-form specific.
Hopefully someone else will be able to explain things better than I
have.
-Doug
On Sep 17, 1:17 pm, "Ian Lawrence" <[EMAIL PROTECTED]> wrote:
> Hi,
> I would like each uploaded test to go into a separate directory in my
> media_root. In models.py I have:
>
> class TestDetail(models.Model):
> test_grouping = models.ForeignKey(TestGrouping)
> test_name = models.CharField(maxlength=200)
> python_file = models.FileField("Python File",
> upload_to='tests/%s/' % test_name)
> control_file = models.FileField("Control File",
> upload_to='tests/%s/' % test_name)
> resources = models.FileField("Resource", upload_to='tests/%s/' %
> test_name)
> def __unicode__(self):
> return self.test_name
>
> class Admin:
> list_display = ('test_name',)
>
> but in the admin this gives me
> home/ian/Web/media/tests/<django.db.models.fields.CharField object at
> 0x8a3f68c>/fsfuzzer.py
> when i try to upload files. This is probably very simple to solve but
> i have looked through the docs and googled and nothing seems obvious
> yet
> thanks
> Ian
> --http://ianlawrence.info
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---