我有不同的類別,如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")
「我想在文件上傳到磁盤之前驗證它」如何?該文件只能通過實際檢查字節來驗證。如何在沒有上傳到可以檢查字節的計算機上的情況下檢查字節? –