I did miss that thanks!

However, I now seem to have an issue with the def my_upload_to(instance, 
filename) line.

Apparently "self" nor "instance" works here, and so I am getting the error 
1 argument given when I need "two".

@staticmethod doesn't seem to want to work properly either.

Thanks again!

On Thursday, December 5, 2013 9:10:13 AM UTC-5, Daniel Roseman wrote:
>
> On Thursday, 5 December 2013 12:23:52 UTC, Matt Lind wrote:
>>
>> Ok, I get what what you're saying, but I seem to be having a heck of a 
>> time getting the randDir passed into the instance to perform the upload.
>>
>> Here is the new code snippets:
>>
>> views.py
>> instance = Upfile( file = file)
>> instance.upload(randDir)
>>
>> models.py
>>
>> @staticmethod
>> def upload(randDir)
>>    file = models.FileField( upload_to = MEDIA_ROOT+'/'+randDir)
>>
>> I am using @staticmethod as I found that otherwise I needed to pass 
>> "self" and kept getting a NameError "self" not defined.
>>
>> The error I currently get with the above code is:
>>
>> TypeError: 'file' is an invalid keyword argument for this function
>>
>> When clicking my "upload" button.
>>
>> Thanks
>>
>
>
> You can't do that: model fields need to be defined in the model itself, at 
> declaration time.
>
> But you seem to have missed the bit in the documentation[1] which 
> describes how upload_to can be a callable, which does exactly what you want.
>
> def my_upload_to(instance, filename)
>     directory = "".join(random.choice(string.ascii_letters) for n in 
> xrange(12))
>     return os.path.join(MEDIA_ROOT, directory, filename)
>
> class UpFile(models.Model):
>     file = models.FileField(upload_to=my_upload_to)
>
>
> [1]: 
> https://docs.djangoproject.com/en/1.6/ref/models/fields/#django.db.models.FileField.upload_to
>
> --
> DR.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/879ed008-64fd-4901-8ffc-dfa167eaaf91%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to