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