On Tue, Apr 14, 2009 at 4:04 PM, Vincent Foley <vfo...@gmail.com> wrote:

>
> Hello,
>
> I want to be able to add files inside a model to test that model.
> However, I'm unable to find in the documentation any mention on how to
> do so.  I asked on IRC and somebody suggested the following:
>
> from django.core.files import File
> from myproject.myapp.models import MyModel
> fd = open('/path/to/file')
> f = File(fd)
> obj = MyModel.objects.create(file_field=f)
> f.close()
> fd.close()
>
>
> However, when I try to access obj, I get the following exception:
> SuspiciousOperation: Attempted access to '/path/to/file' denied.
>
> Is it how one is supposed to "attach" file into a model?  If so, what
> could be causing my problem?
>
> Thanks,
>
> Vincent
>
> >
>
If the file isn't already relative to your MEDIA_ROOT you need to do:

m = MyModel()
m.file_field.save('new_file_name', open('/path/to/file').read()).

Alex
-- 
"I disapprove of what you say, but I will defend to the death your right to
say it." --Voltaire
"The people's good is the highest law."--Cicero

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to