保存对象时 Django mod_wsgi PicklingError

发布于 2024-09-10 12:08:51 字数 3596 浏览 6 评论 0原文

您知道对此有什么解决方案吗:

[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162] mod_wsgi (pid=3072): Exception occurred processing WSGI script '/home/www/shop/django.wsgi'., referer: http://shop.domain.com/accounts/checkout/?
[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162] Traceback (most recent call last):, referer: http://shop.domain.com/accounts/checkout/?
[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162]   File "/usr/lib/python2.5/site-packages/django/core/handlers/wsgi.py", line 245, in __call__, referer: http://shop.domain.com/accounts/checkout/?
[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162]     response = middleware_method(request, response), referer: http://shop.domain.com/accounts/checkout/?
[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162]   File "/usr/lib/python2.5/site-packages/django/contrib/sessions/middleware.py", line 36, in process_response, referer: http://shop.domain.com/accounts/checkout/?
[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162]     request.session.save(), referer: http://shop.domain.com/accounts/checkout/?
[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162]   File "/usr/lib/python2.5/site-packages/django/contrib/sessions/backends/db.py", line 57, in save, referer: http://shop.domain.com/accounts/checkout/?
[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162]     session_data = self.encode(self._get_session(no_load=must_create)),, referer: http://shop.domain.com/accounts/checkout/?
[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162]   File "/usr/lib/python2.5/site-packages/django/contrib/sessions/backends/base.py", line 88, in encode, referer: http://shop.domain.com/accounts/checkout/?
[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162]     pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL), referer: http://shop.domain.com/accounts/checkout/?
[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162] PicklingError: Can't pickle <class 'decimal.Decimal'>: it's not the same object as decimal.Decimal, referer: http://shop.domain.com/accounts/checkout/?

使用 DecimalField :/ 保存模型实例时有时会发生这种情况。

views.py:

def checkout_authenticated(request):
    order = get_order(request)
    user = request.user

    if request.method == 'POST':
        form = OrderCheckoutForm(request.POST, instance = order)
        if form.is_valid():
            form.save()
...

forms.py:

class OrderCheckoutForm(forms.ModelForm):
    class Meta:
        model = Order
        exclude = ('status',
                   'user')

models.py:

class Shipping(models.Model):
    name = models.CharField(max_length = 256)
    price = models.DecimalField(max_digits = 10, decimal_places = 2)
    description = models.TextField(blank = True, null = True)
    cash_on_delivery = models.BooleanField(default = False)

class Order(models.Model):
    date = models.DateField(editable = False, auto_now_add=True)
    status = models.CharField(max_length = 1, choices = STATUS, default = Status.NEW)

    shipping = models.ForeignKey(Shipping, related_name = 'orders', null = True)
    address = models.ForeignKey(Address, related_name = 'address_order', null = True)
    invoice = models.BooleanField(default = False)
    company = models.ForeignKey(Company, related_name = 'company_order', blank = True, null = True)

我认为原因是:

price = models.DecimalField(max_digits = 10, decimal_places = 2)

提前致谢, 埃塔姆。

Do you know any solution to this:

[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162] mod_wsgi (pid=3072): Exception occurred processing WSGI script '/home/www/shop/django.wsgi'., referer: http://shop.domain.com/accounts/checkout/?
[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162] Traceback (most recent call last):, referer: http://shop.domain.com/accounts/checkout/?
[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162]   File "/usr/lib/python2.5/site-packages/django/core/handlers/wsgi.py", line 245, in __call__, referer: http://shop.domain.com/accounts/checkout/?
[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162]     response = middleware_method(request, response), referer: http://shop.domain.com/accounts/checkout/?
[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162]   File "/usr/lib/python2.5/site-packages/django/contrib/sessions/middleware.py", line 36, in process_response, referer: http://shop.domain.com/accounts/checkout/?
[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162]     request.session.save(), referer: http://shop.domain.com/accounts/checkout/?
[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162]   File "/usr/lib/python2.5/site-packages/django/contrib/sessions/backends/db.py", line 57, in save, referer: http://shop.domain.com/accounts/checkout/?
[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162]     session_data = self.encode(self._get_session(no_load=must_create)),, referer: http://shop.domain.com/accounts/checkout/?
[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162]   File "/usr/lib/python2.5/site-packages/django/contrib/sessions/backends/base.py", line 88, in encode, referer: http://shop.domain.com/accounts/checkout/?
[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162]     pickled = pickle.dumps(session_dict, pickle.HIGHEST_PROTOCOL), referer: http://shop.domain.com/accounts/checkout/?
[Thu Jul 08 19:15:38 2010] [error] [client 79.162.31.162] PicklingError: Can't pickle <class 'decimal.Decimal'>: it's not the same object as decimal.Decimal, referer: http://shop.domain.com/accounts/checkout/?

It occures sometimes while saving model instance with DecimalField :/.

views.py:

def checkout_authenticated(request):
    order = get_order(request)
    user = request.user

    if request.method == 'POST':
        form = OrderCheckoutForm(request.POST, instance = order)
        if form.is_valid():
            form.save()
...

forms.py:

class OrderCheckoutForm(forms.ModelForm):
    class Meta:
        model = Order
        exclude = ('status',
                   'user')

models.py:

class Shipping(models.Model):
    name = models.CharField(max_length = 256)
    price = models.DecimalField(max_digits = 10, decimal_places = 2)
    description = models.TextField(blank = True, null = True)
    cash_on_delivery = models.BooleanField(default = False)

class Order(models.Model):
    date = models.DateField(editable = False, auto_now_add=True)
    status = models.CharField(max_length = 1, choices = STATUS, default = Status.NEW)

    shipping = models.ForeignKey(Shipping, related_name = 'orders', null = True)
    address = models.ForeignKey(Address, related_name = 'address_order', null = True)
    invoice = models.BooleanField(default = False)
    company = models.ForeignKey(Company, related_name = 'company_order', blank = True, null = True)

I think the reason is:

price = models.DecimalField(max_digits = 10, decimal_places = 2)

Thanks in advance,
Etam.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枫以 2024-09-17 12:08:51

请参阅这个答案< /a>.这有帮助吗?

编辑(回应您的评论):

恐怕我对 django 或您的代码不太了解,无法为您提供解决方案。不过,我确实对潜在的错误有了更清晰的了解:在发生此错误之前,创建了一个 decimal.Decimal 实例,然后由于某种原因创建了 decimal.Decimal 类> 被重新定义。当 pickle 类无法按名称找到给定对象的类定义时,它不起作用。

这是一个显示类似问题的解释器会话:

>>> import cPickle
>>> class Foo(object):
...     pass
... 
>>> f = Foo()
>>> s = cPickle.dumps(f)
>>>
>>> # Redefine class Foo 
>>> class Foo(object):
...     pass
... 
>>> # Now attempt to pickle the same object that was created with the old Foo class
>>> s = cPickle.dumps(f)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cPickle.PicklingError: Can't pickle <class '__main__.Foo'>: it's not the same object as __main__.Foo
>>>
>>> # Create an object with the new Foo class, and try to pickle it (this works)
>>> f2 = Foo()
>>> s = cPickle.dumps(f2)

See this answer. Does that help?

EDIT (responding to your comment):

I'm afraid I don't know either django or your code well enough to give you a fix. I do have a clearer idea of the underlying error, though: Before this error occurred, an instance of decimal.Decimal was created, and then for some reason the class decimal.Decimal was re-defined. The pickle class doesn't work when it can't locate, by name, the class definition for a given object.

Here's an interpreter session showing a similar problem:

>>> import cPickle
>>> class Foo(object):
...     pass
... 
>>> f = Foo()
>>> s = cPickle.dumps(f)
>>>
>>> # Redefine class Foo 
>>> class Foo(object):
...     pass
... 
>>> # Now attempt to pickle the same object that was created with the old Foo class
>>> s = cPickle.dumps(f)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
cPickle.PicklingError: Can't pickle <class '__main__.Foo'>: it's not the same object as __main__.Foo
>>>
>>> # Create an object with the new Foo class, and try to pickle it (this works)
>>> f2 = Foo()
>>> s = cPickle.dumps(f2)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文