Let us illustrate this with an example.
Let's say I want to create the FileUploader API where it will store fields such as id, file_path, file_name, size, owner, etc. in the database. See an example model below:
class FileUploader(models.Model): file = models.FileField() name = models.CharField(max_length=100)
Now, for the API, this is what I want:
GET: When I run the GET endpoint, I want all of the above fields to be loaded for each downloaded file.
POST: But so that the user can create / upload a file, why should she worry about passing all these fields. It can just load the file, and then, I suppose, the serializer can get the remaining fields from the loaded FILE.
Searilizer: Question: I created a serializer below to serve my purpose. But not sure if its the right way to implement.
class FileUploaderSerializer(serializers.ModelSerializer):
Permission for reference:
class FileUploaderViewSet(viewsets.ModelViewSet): serializer_class = FileUploaderSerializer parser_classes = (MultiPartParser, FormParser,)
Also, another question: I want the user to provide an additional parameter called 'overwrite' (if the file already exists on the server).
I am not sure how to access this in a serializer.
source share