我有不同的类别,例如English
,French
,我已应用key
language = models.ForeignKey(Category)
我想在上传到磁盘之前验证该文件。
我想如果类别为english
,则文件上传到english/album_name
,如果类别为french
,则文件上传到french/album_name
。我写了forms.py
个文件。但不知道。提前致谢
Models.py
class Artist(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(unique = True,max_length=100,help_text="Suggested value automatically generated from name. Must be unique.")
class Album(models.Model):
name = models.CharField(max_length=100)
slug = models.SlugField(unique = True,max_length=100,help_text="Suggested value automatically generated from name. Must be unique.")
path = models.CharField(max_length=100,null=True, blank=True)
language = models.ForeignKey(Category)
albumid = models.CharField(max_length=100)
class Song(models.Model):
title = models.CharField(max_length=100)
artist = models.ManyToManyField(Artist)
music = models.ForeignKey(Music)
album = models.ForeignKey(Album)
file = models.FileField(upload_to='media/mp3_files')
forms.py
from django import forms
from db.song.models import Song
class SongAdminForm(forms.ModelForm):
class Meta:
model = Song
# No idea what to do next :-?
def clean_file(self):
file = self.cleaned_data["file"]
if file:
if file._size > 10*1024*1024:
raise ValidationError("Audio file too large ( > 10mb )")
if not file.content-type in ["audio/mpeg","audio/..."]:
raise ValidationError("Content-Type is not mpeg")
答案 0 :(得分:0)
我认为你真正想要的是在上传后验证它,但在真正保存之前。
上传文件时,它们位于临时文件夹中,因此您可以在将其保存到相应文件夹之前进行所需的所有检查。
但是,如果你真的是指上传前检查,你必须使用Javascript(因为它是客户端)。但是,不要忘记你应该“永远不要信任用户输入”,这包括Javascript的功能,因为用户可以更改Javascript代码。