- I have a model with a
FileField
namelyfile_test
- I would like to manually create a file and assign in to
file_test
in the serializer Below are my code, would like some help. Thanks
class Test(models.Model): file_test = models.FileField(storage=OverwriteStorage(), upload_to=_get_landmark_upload_path, max_length=100, blank=True) class TestSerializer(serializers.ModelSerializer): test = serializers.CharField(write_only=True, required=True) class Meta: model = Test fields = ('file_test','test') def save(self, *args, **kwargs): self.file_test.save('test.txt', ContentFile(self.validated_data['test'])) return super().save(*args, **kwargs)
Or is there a way to create a file and assign it to
file_test
?example in serializer
def save(self, *args, **kwargs): kwargs['file_test'] = ?????(how to create this file) return super().save(*args, **kwargs)
Asked
Active
Viewed 590 times
0
-
https://stackoverflow.com/questions/50575902/saving-base64imagefield-type-using-django-rest-saves-it-as-raw-image-how-do-i-c/50693479#50693479 Use this solution `Base64ImageField` – Anup Yadav Jan 25 '19 at 06:58
1 Answers
1
- I'd manage to solve the issue by using ContentFile
Look at example below
def save(self, *args, **kwargs): kwargs['file_test'] = ContentFile( self.validated_data['test'], name="test.txt") return super().save(*args, **kwargs)

nicker
- 477
- 2
- 6
- 20