4

I want to let each user upload a text file such that he can see only his uploaded file. I also want his uploaded files to get stored in his own folder like home/ubuntu/documents/username/

my models.py looks like this -

from django.db import models
from django.contrib.auth.models import User
import os

class UserProfile(models.Model):

user = models.OneToOneField(User)

    def __unicode__(self):
        return self.user.username


def get_upload_path(instance, filename):
    return os.path.join('documents', instance.owner.username, filename)

class Document(models.Model):
    docfile = models.FileField(upload_to=get_upload_path)
Vipul Vaibhaw
  • 59
  • 1
  • 6

1 Answers1

3

EDIT

Sorry about the last faux pas.

def get_upload_path(instance, filename):
    return 'documents/{0}/{1}'.format(instance.user.username, filename)

class Document(models.Model):
    user = models.ForeignKey(User, null=True)
    docfile = models.FileField(upload_to=get_upload_path)

Also, see documentation for another example.


Notes

  1. Do configure MEDIA_ROOT path in your settings file. This is where the files will be stored in.
  2. Don't forget to write another method which will change the name of the folder of a user whenever he/she will change his/her username.
  3. Alternative to Point 2 above will be using pk instead of username. But it's up to you.
xyres
  • 20,487
  • 3
  • 56
  • 85
  • Hey @xyres when i ran this code, i got following error - NameError: name 'get_upload_path' is not defined – Vipul Vaibhaw Jun 06 '15 at 20:01
  • @VipulVaibhaw Sorry, made a mistake. Fixed it now. – xyres Jun 06 '15 at 20:10
  • It now says You are trying to add a non-nullable field 'owner' to document without a default. I am sorry, I am new to django, so getting a lot of problems. – Vipul Vaibhaw Jun 06 '15 at 20:17
  • @VipulVaibhaw Does your `Document` model has `user` field, or any other `ForeignKey` linking to `User` model? If unclear, just post your whole `Document` model in your question. I'll figure out. – xyres Jun 06 '15 at 20:21
  • I have posted whole models.py file here, no user in Document model. – Vipul Vaibhaw Jun 06 '15 at 20:25
  • @VipulVaibhaw Well, then you'll have to add a `ForeignKey` to `User` model in your `Document` model. Otherwise you won't know which user has uploaded what file. Instead of `owner`, write `user = ForeignKey(User, null=True)`. I've updated the code in answer, too. – xyres Jun 06 '15 at 20:31
  • I ran your code but now I am getting this error - AttributeError at /list/ 'NoneType' object has no attribute 'username' /home/ubuntu/test_site.com/testproject/testapp/models.py in get_upload_path, line 22 – Vipul Vaibhaw Jun 06 '15 at 20:41
  • @VipulVaibhaw When you upload a file, you'll have to choose a user too. That's how you'll know which file belongs to which user. If you're doing this in admin, this should be easy. If not, then that's a totally different question. – xyres Jun 06 '15 at 20:50
  • but why to choose a user? the user logged in should be able to upload his file in his own folder. – Vipul Vaibhaw Jun 06 '15 at 20:54
  • @VipulVaibhaw Yes I get what you're saying. That's why I said it's a different question. This question might be helpful - http://stackoverflow.com/questions/2991365/how-to-auto-insert-the-current-user-when-creating-an-object-in-django-admin – xyres Jun 06 '15 at 20:58