Save a file in model without using Django forms

If you want to store the document without using Django forms then you can use below code : 

in models.py

...
class UserProfile(models.Model) :
    user = models.OneToOneField(User)
    doc = models.FileField(upload_to='uploads/', blank=True)

in python script

from core.models import UserProfile
from django.core.files import File

user1 = UserProfile(pk = 1)
user1.doc.save('resume.pdf', File(open('/tmp/resume.pdf', 'r')))

Comments

Popular posts from this blog

What is composite key?

What are the different data types in Python?

What is __repr__ function?