Reputation: 3522
class Post(models.Model):
id = models.CharField(max_length=36, primary_key=True, default=uuid.uuid4())
profile = models.ForeignKey(Profile)
created_at = models.BigIntegerField(default=int(time.time()))
edited_at = models.BigIntegerField(default=int(time.time()))
image = models.FileField(upload_to='/images/')
title = models.CharField(max_length=50)
description = models.CharField(max_length=250)
class MultiPartResource(object):
def deserialize(self, request, data, format=None):
if not format:
format = request.Meta.get('CONTENT_TYPE', 'application/json')
if format == 'application/x-www-form-urlencoded':
return request.POST
if format.startswith('multipart'):
data = request.POST.copy()
data.update(request.FILES)
return data
return super(MultiPartResource, self).deserialize(request, data, format)
class PostResource(MultiPartResource, ModelResource):
profile = fields.ForeignKey(ProfileResource, 'profile')
class Meta:
queryset = Post.objects.all()
resource_name = 'post'
allowed_methods = ['get', 'post']
authorization = Authorization()
curl -F profile="/api/profile/e52f5ddc-d005-4d22-ae49-984a32012fdd/" -F image="pic2.jpg" -F title="post" -F description="some description" http://localhost/api/post/ -v
The object is created in the database with the filename, but there is no file on disk.
Upvotes: 2
Views: 1203
Reputation: 3522
// PostResource needed an additional field and the value of attribute should match the field name in the model
image = fields.FileField(attribute='image')
//The upload_to parameter should contain a location that already exists or you will get an error
image = models.FileField(upload_to='/images/')
//The image field used in the curl command needed to be changed to look like this
-F "[email protected]"
If anyone else has a problem getting this to work I hope you stumble across this post and find your solution.
Upvotes: 2