138
138
First, import the items you will need::
140
141
from django.core.files import File
141
142
from attachments.models import Attachment
142
143
from myproject.models import Person
144
145
Next, retrieve the object you wish to attach to::
145
147
me = Person.objects.get(name='aaron')
147
149
Now open the attachment you want from your drive using the django File object::
148
151
mypicture = File(open('/home/aaron/mypicture.jpg', 'r'))
150
153
Finally, create the Attachment object and save it::
153
157
a.attachment_file = mypicture