Search code examples
pythondjangomodels

Adding FieldFile objects without form in django


I have a django model as follow:

class XML(ExtensibleModel):
     xml = models.FileField(upload_to='xml',blank=True, null=True)

Here, I store some xmls files. Before, I submited the files to my server by a html form. Now, I copy the files by ssh, and I want to keep storing the new files in this model. The problem is that I can't do it. I tried with the follow code

f = open(FILENAME,'r')
A = XML(xml = f)
A.save()

but, I get this error:

'file' object has no attribute '_committed'

Any idea?


Solution

  • Try using a django file instead of just an open file.

    from django.core.files import File
    ...
    f = open(FILENAME,'r')
    A=XML()
    A.xml.save(filename, File(f), save=True)
    A.save()