我正在使用django 1.11
+ rest_framework
尝试使用lz4
python
库实现无损压缩方法。简而言之,我实现了一个基于类的视图POST
方法,该方法定义了压缩方法。我后来想要在GET
请求完成时实现解压缩方法。当我尝试发出POST
请求时,我得到TypeErrorr
log:
a bytes-like object is required, not 'InMemoryUploadedFile'
我并不熟悉无压缩压缩,一些正确的方向会很好。最后,还有其他方法可以在POST
和GET
方法上实现文件的压缩和解压缩吗?
我的模型如下:
class EmployeeDocument(models.Model):
"""
Model, Which holds Employee documents uploaded.
"""
employee = models.ForeignKey(
Employee, related_name="employee_docs", null=True, blank=True)
document = models.FileField(
upload_to='Images/', default='Images/None/No-img.jpg')
details = models.TextField()
# url = models.URLField(null=True)
# path_to_image = models.CharField(null=True, max_length=100)
# upload_date = models.DateTimeField(auto_now=True, db_index=True)
def __str__(self):
return 'DOC - %s' % (self.employee.user.username)
然后 Views.py
class EmployeeDocumentPOST(APIView):
"""
compress Document
"""
permission_class = [AllowAny]
serializer_class = EmployeeDocumentSerializer
def compressed_doc(self,document):
document = lz4.frame.compress(document)
return document
def post(self, request, *args, **kwargs):
compressd_doc = self.compressed_doc(document)
employee = Employee.objects.get(id=request.data.pop('employee'))
detail = request.data.get('detail')
employee_doc = EmployeeDocument.objects.create(**request.data)
employee_doc.compressed_doc = compressed_doc
employee_doc.employee = employee
employee_doc.detail = detail
employee_doc.save()
serializer = EmployeeDocument(employee_doc)
return Response(serializer.data, status = status, status = status.HTTP_200_OK)