mercredi 27 mai 2015

Django Rest framework + EmberJS File upload update(patch)

I have a model like this:

// models.py
class MyModel(models.Model):
    name = models.CharField(max_length=30, null=True, blank=True)
    someboolean = models.BooleanField(default=False)
    someotherBoolean = models.BooleanField(default=False)
    myfilefield = models.FileField(upload_to='/files/')

Then i have a serializer like this:

// serializers.py
class MyModelSerializer(serializers.ModelSerializer):
    class Meta:
        model = MyModel

Then i have a View which inherit from RetrieveUpdateDestroyAPIView

This Django Rest setup is connected to the EmberJS and is running ok. The data from the server is retrived without any problem and the myfilefield content is represented like a string in ember which is a url to the actual file. When i think of uploading a file to the field on the Django Rest side it seems that i have to play a little with FileUploadParser to make it right and i think this is not much more lines of code into it.

But the issue is that my model on the Ember side should be updated in parts. Sometimes i need to update only the value of someboolean and do not send any files to myfilefield either because the file is already there or because this is done in the other UI iteration (separately). When in Ember i call this.get('model').save(); after some change to the someboolean is made it sends the whole model data back to django rest and the myfilefield in this json request is represented as a string and not as file thus the server returns an error.

As i understand there could be some workarounds to this situation:

  1. The first would be to create custom serializers.CustomFileField which checks whether the provided string is equal to the url which is generated by the serializer on the output and if it is just leaves the file intact.
  2. Second option would be to somehow implement the quick and dirty patch on the ember side which as i understand is still thing under development according to This Link. But this option seems to be quite hard as i have a lot of models with filefields and i should implement the patch method in ember for each and every one of them.
  3. The third option as i forsee would be to create a special File model like so:

    //models.py class File(models.Model): filebody = models.FileField(upload_to='/files/')

and make the myfilefield on the MyModel read_only so it won't validate at any time. I could also implement some method on the File model which would recieve the model and instance for which this file really belongs and after upload and validation would make the myfilefield = filebody This method also seems very dirty but at least keeps some concepts abstracts so i wouldn't need to worry how many models with FileFields i actually have in my project.

My UI expects the user to do the change to one model field at a time and then save the model - i.e. 1. change the name field, save the model.
2. change the boolean field, save the model. 3. upload file, save the model.

Any suggestions on what would be the best way in terms of django rest to accomplish this considering the fact that ember still does not support PATCH requests.




Aucun commentaire:

Enregistrer un commentaire