Processing file uploads before object is saved

后端 未结 3 1406
情书的邮戳
情书的邮戳 2020-12-30 16:17

I\'ve got a model like this:

class Talk(BaseModel):
  title        = models.CharField(max_length=200)
  mp3          = models.FileField(upload_to = u\'talks/         


        
3条回答
  •  Happy的楠姐
    2020-12-30 17:22

    a cleaner way to get the file before be saved is like this:

    from django.core.exceptions import ValidationError
    
    #this go in your class Model
    def clean(self):
        try:
            f = self.mp3.file #the file in Memory
        except ValueError:
            raise ValidationError("A File is needed")
        f.__class__ #this prints 
        processfile(f)
    

    and if we need a path, ther answer is in this other question

提交回复
热议问题