本文介绍了在cacheops中用二进制字段酸洗django模型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  class MyModel(models.Model )
bin_data = models.BinaryField()

从我的单位测试的上下文,我请执行以下操作:

  import pickle 
tmp_obj = MyModel.objects.create(bin_data =12345)
obj = MyModel.objects.get(pk = tmp_obj.pk)#从DB加载
data = pickle.dumps(obj)
obj2 = pickle.loads(data)

但是pickle.dumps()失败:

  TypeError:不能pickle缓冲区对象

当我使用以下命令要泡菜:

  data = pickle.dumps(obj,protocol = -1)

转储成功,但pickle.loads()失败:

 code> TypeError:buffer()至少需要一个参数(0)

涉及专业我正在使用我正在使用的django-cacheops库来缓存我的查询。



在引擎下,django-cacheops使用pickle.dumps(obj ,protocol = -1),并且我收到与上述pickle.loads()相同的错误。



我将不胜感激腌汁问题和django-cacheops问题。



谢谢

解决方案

href =https://pypi.python.org/pypi/django-cacheops/2.1.1 =nofollow> cacheops 2.1.1 。



使用这种方式使用缓冲区的外部酸洗功能:

  import copy_reg 
copy_reg.pickle(buffer,lambda b :(buffer,(bytes(b),)))


I have a simple Django model with a binary field that I would like to pickle.

class MyModel(models.Model):
    bin_data = models.BinaryField()

From the context of my unittests, I do the following:

import pickle
tmp_obj = MyModel.objects.create(bin_data="12345")
obj = MyModel.objects.get(pk=tmp_obj.pk)  # load from DB
data = pickle.dumps(obj)
obj2 = pickle.loads(data)

However the pickle.dumps() fails with:

TypeError: can't pickle buffer objects

When I use the following command to pickle:

data = pickle.dumps(obj, protocol=-1)

The dump succeeds but pickle.loads() fails with:

TypeError: buffer() takes at least 1 argument (0 given)

This actually relates to a problem I'm having with the django-cacheops library which I'm using in order to cache my queryset.

Under the hood django-cacheops uses pickle.dumps(obj, protocol=-1), and I receive the same error as described above for the pickle.loads()

I would appreciate an answer for both the pickle issue and the django-cacheops issue.

Thanks

解决方案

Fixed in cacheops 2.1.1.

Using external pickling function for buffer this way:

import copy_reg
copy_reg.pickle(buffer, lambda b: (buffer, (bytes(b),)))

这篇关于在cacheops中用二进制字段酸洗django模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 00:47