The syntax to add it in Model is:
class MyModel(models.Model): video = FileField(upload_to=None[, max_length=100, **options])
Here upload_to is local filesystem path that will be appended to your MEDIA_ROOT setting to determine the value of the url(mymodel.video.url) attribute.
Setting MEDIA_ROOT and upload_to
MEDIA_ROOT = â€˜/home/myname/files/
results in: /home/myname/files/videos/abc.flv
where as if,
MEDIA_ROOT = ‘/home/myname/files/’
and file : abc.flv
result will be saved in : ‘/videos/abc.flv’ (i.e., root directory of filesystem)
You can also set upload_to=’videos/%Y/%m/%d’. The ‘%Y/%m/%d’ part of upload_to is strftime formatting; ‘%Y’ is the four-digit year, ‘%m’ is the two-digit month and ‘%d’ is the two-digit day. If you upload a file on Jan. 15, 2007, it will be saved in the directory /home/myname/files/videos/2007/01/15.
Now here’s how would you save the actual data.
from django.core.files.base import ContentFile def save_file(request): mymodel = MyModel.objects.get(id=1) file_content = ContentFile(request.FILES['video'].read()) mymodel.video.save(request.FILES['video'].name, file_content)
Hope the code is self explanatory. For further help do drop comment.