18

User is uploading a .c file of a particular question. I want the file to be renamed as 'userid_questionid.c'

My models.py is :

from django.db import models

class users(models.Model):
    username = models.CharField(max_length=20)
    password = models.CharField(max_length=20)
    score=models.IntegerField(max_length=3)
    def __unicode__(self):
        return self.username

class questions(models.Model):
    question = models.TextField(max_length=2000)
    qid=models.IntegerField(max_length=2)
    def __unicode__(self):
       return self.qid

def content_file_name(instance, filename):
    return '/'.join(['uploads', instance.questid.qid, filename])


class submission(models.Model):
    user = models.ForeignKey(users)
    questid = models.ForeignKey(questions)
    file = models.FileField(upload_to=content_file_name)

I tried this. But it just creates the folder of user and saves file in it. Please help. Thank You. I need the file to be renamed.

pacholik
  • 8,607
  • 9
  • 43
  • 55
cold_coder
  • 584
  • 4
  • 8
  • 24
  • The delimiter isn't always `/`. Depending on the OS, `.`, `>`, `:`, and backslashes may be also used. – sgarza62 Sep 03 '14 at 21:56
  • Possible duplicate of [How to change the file name of an uploaded file in Django?](https://stackoverflow.com/q/2680391/608639), [Choose the filename of an uploaded file with Django](https://stackoverflow.com/q/1237602/608639), [Django File Upload and Rename](https://stackoverflow.com/q/25652809/608639), [Rename file on upload to admin using Django](https://stackoverflow.com/q/30167409/608639), etc. – jww Sep 21 '18 at 09:23

1 Answers1

37

You just need to change your content_file_name function. The function below will create paths like so: uploads/42_100.c, where 42 is the user's id, and 100 is the question's id.

import os
def content_file_name(instance, filename):
    ext = filename.split('.')[-1]
    filename = "%s_%s.%s" % (instance.user.id, instance.questid.id, ext)
    return os.path.join('uploads', filename)
sgarza62
  • 5,998
  • 8
  • 49
  • 69
  • It worked! Also I had to make modifications in views.py and the django template associated with it.Thanks. – cold_coder Sep 04 '14 at 13:21
  • @JaineshPatel Glad to hear it! Please mark the answer as correct if the question is resolved – sgarza62 Sep 04 '14 at 13:32
  • Now I need the file in format "submissionid_userid_questionid.c" ! I tried using "instance.code.id" but its not working. I also tried "instance.id" but its also not working. Please look after it. – cold_coder Sep 05 '14 at 17:45
  • @JaineshPatel You're calling `upload_to = content_fine_name` from the `file` field of your`submission` model, correct? Then you should change the line of my solution to: `filename = "%s_%s_%s.%s" % (instance.id, instance.user.id, instance.questid.id, ext)`. – sgarza62 Sep 05 '14 at 19:28
  • @JaineshPatel If that does not work, open a question with a full report (including errors), and link to it here in the comments so I can see it. – sgarza62 Sep 05 '14 at 19:29