0

i am doing unit tests on my rest framework API, my model:

class attach():
    attachment = models.ImageField
    name = models.CharField

then i attempt to post to is via the API i defined(standard serializer not shown here)

c = Client()
with open('wishlist.doc') as fp:
     c.post('/customers/wishes/', {'name': 'fred', 'attachment': fp}, format='multipart')

i get the following stack trace

ValueError: The 'attachment' attribute has no file associated with it.

how do i associate the key attachment with the file

Dr Manhattan
  • 13,537
  • 6
  • 45
  • 41

1 Answers1

0

Make sure to set format="multipart" when creating the request.

[...]
c.post('/customers/wishes/', {'name': 'fred', 'attachment': fp}, format='multipart')

Source: https://stackoverflow.com/a/27576436/1682844

Community
  • 1
  • 1
mishbah
  • 5,487
  • 5
  • 25
  • 35