0

I am using django 1.4 and using django model's filefield to upload some document via modelform. I am having following issues:

When I submit form it says:

Data truncated for column 'file_name' at row 1

Following is my model for this:

class App(models.Model):
    user_name=models.CharField(max_length=50)
    email=models.CharField(max_length=50)
    status=models.CharField(max_length=10,choices=APPLICATIONSTATUSCHOICE)
    archived=models.BooleanField()
    mark_spam=models.BooleanField()
    date=models.DateField()
    file_name=models.FileField(upload_to=PATH+"/")
    def getPath(self):
        return PATH
    def __unicode__(self):
        return self.user_name
    def send_email(self):
        pass

Here is the code for model form:

class AppForm(ModelForm):
    class Meta:
     model=App
         exclude=('status','archived','mark_spam')

email=forms.EmailField()

    def save(self,commit=True):
         app=super(AppForm,self).save(commit=False)
     app.status='sent'
         app.save()

Also it is storing file with its original name,can I have it with something unique name as I am from PHP background and in PHP I normally save it like <mysql auto id>.<filextension>, so how can I do it in django. My first impression was that all this will be automatically done via django while it just save it with name of its own choice but I need to save names into db also so want to name them according to my choice. How can it be done and what is problem in my code that is giving above mentioned error?

4

1 回答 1

2

file_name您要存储多长时间?

默认情况下,FileField实例在数据库中创建为 varchar(100) 列。与其他字段一样,您可以使用 max_length 参数更改最大长度。这可能会导致数据截断错误。

您也可以根据需要重命名文件。upload_to可以是一个可调用的,它将实例和上传文件的名称作为输入,然后您可以指定要存储的确切名称和路径。

例如。

def get_file_name(instance, filename):
    return '/'.join(['blah', instance.user.username, filename])

...
    file_name=models.FileField(upload_to=get_file_name)
...
于 2012-10-07T09:46:51.210 回答