Skip to content

Instantly share code, notes, and snippets.

@tomchristie
Last active December 20, 2015 12:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomchristie/6130319 to your computer and use it in GitHub Desktop.
Save tomchristie/6130319 to your computer and use it in GitHub Desktop.
def get_upload_to(instance, filename):
user = instance.user
return 'uploads/%s/%s' % (user.username, filename)
class MyModel(models.Model):
upload = models.FileField(upload_to=get_upload_to)
...
def upload(request):
obj = MyModel()
obj.user = request.user
obj.upload = request.FILES['upload']
obj.save()
...
@urbanslug
Copy link

You gave me this on #django but I ran into a snag I don't even understand.

May you please help with https://gist.github.com/urbanslug/6143884 I get the error: << 'User' object has no attribute 'rfind' >>

Traceback at: http://pastebin.com/VmA2zLEk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment