Django - having different MEDIA_ROOT for different users -


I am using Django 1.5 and I want users to upload photos. I want the photo to be stored at a different location, depending on what the User ID is. For example, if a user uploads a file and the user ID is 1 (the ID / unique primary key that is given to each user when the user object is made from the Digengo Generic User Model), then I want to save the image. I think the directory

  myPorjectFolder / myApp / static / site_media / images / 1 (user ID) / uploaded photo  

In this way I think that I do it In Settings, add this line:

  MEDIA_ROOT = '/ home / userName / myProject / myApp / site_media / static / images /'  

In models.py, model this way: Import from django.db import model from django.contrib.auth.models User class UserImages (models.Model): user = models .onomoboyfield (user) photo = model. Imagefield (upload_to = current user id / upload photo) # I know this will not work .. How will I work for it?

Am I right? Or is this completely different from what I am thinking?

Edit: My forms are:

  class UploadImageForm (forms.Form): image = Forms.ImageField ()  

If you want to use the user's ID, you can create a function to upload the image like this: < /p>

  def get_file_path (example, filename): return os.path.join ('% s / uploaded photos'% instance.user_id, filename)  

Then in the image field of your model you will

  class UserImages (models.Model): user = models.ForeignKeyField (user) photo = models.ImageField (Upload_to = get_fi) Le_path)  

Also keep in mind that I have used a foreign keyfield for user property.

Edit:

For your form use, you can:

  form = UploadImageForm (none) .POST) form.instance.user = request.user user_image = form.save ()  

but you will need to convert your forms.py to:

  Category UploadImageForm (forms.ModelForm): Class Meta: Model = User Images = ['Photo']  

Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -