FileField
的模型,即file_test
file_test
下面是我的代码,想要一些帮助。谢谢
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)
或者是否可以创建文件并将其分配给file_test
?
序列化程序中的示例
def save(self, *args, **kwargs):
kwargs['file_test'] = ?????(how to create this file)
return super().save(*args, **kwargs)
答案 0 :(得分:0)
看下面的例子
def save(self, *args, **kwargs):
kwargs['file_test'] = ContentFile(
self.validated_data['test'], name="test.txt")
return super().save(*args, **kwargs)