2
我有一个模型GenericForeignKey
。当致电cache.set(key, trac_obj)
时,它失败。我想知道GenericForeignKey
是不是罪魁祸首?Django cache.set()在具有GenericForeignKey的类上失败,可能是什么原因?
# models.py
class Trac(models.Model):
user = models.ForeignKey(User, related_name="%(class)s", null=False)
content_type = models.ForeignKey(ContentType)
object_id = models.PositiveIntegerField()
content_object = generic.GenericForeignKey('content_type', 'object_id')
class Meta:
unique_together = (('user', 'content_type', 'object_id'),)
# views.py
obj = SomeUserProfile # Django UserProfile (or any other model object)
content_type = ContentType.objects.get_for_model(type(obj))
trac_obj = Trac(user=request.user, content_type=content_type, object_id=obj.pk,
content_object=obj)
trac_obj.save()
cache_key = 'Trac-{0}-{1}-{2}'.format(user.id, content_type.id, obj.id)
cache.set(cache_key, trac_obj)
以下是错误消息:
File ".../python2.7/site-packages/memcache.py", line 751, in _val_to_store_info
pickler.dump(val)
File "/usr/lib/python2.7/copy_reg.py", line 70, in _reduce_ex
raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle instancemethod objects
对不起,如果这是愚蠢的,但你可以告诉你如何创建trac_obj? – 2013-03-19 03:31:51
在这里你去乔希,我增加了更多信息 – un33k 2013-03-19 13:59:31
https://code.djangoproject.com/ticket/20212 – yedpodtrzitko 2013-05-06 15:48:50