Django:debug AttributeError:无法设置属性

时间:2014-03-04 17:15:54

标签: django django-models

注意:我终于找到了这个bug,所以下面的文字对我来说也许很有价值。简短的回答:我决定用我之前定义为@property-method的属性创建一个模型字段。我没有删除@property-method的唯一地方是兰花模型。


经过一些调整并调查我的代码后,我突然收到此错误:AttributeError: can't set attribute。我没有更改Orchid的任何代码,但我现在收到此错误:

>>> orc = Orchid.objects.get(id=1)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "/Users/cole/PycharmProjects/Sites/virtualenvs/orchidislandcapital.com/lib/python2.7/site-packages/django/db/models/manager.py", line 151, in get
    return self.get_queryset().get(*args, **kwargs)
  File "/Users/cole/PycharmProjects/Sites/virtualenvs/orchidislandcapital.com/lib/python2.7/site-packages/django/db/models/query.py", line 301, in get
    num = len(clone)
  File "/Users/cole/PycharmProjects/Sites/virtualenvs/orchidislandcapital.com/lib/python2.7/site-packages/django/db/models/query.py", line 77, in __len__
    self._fetch_all()
  File "/Users/cole/PycharmProjects/Sites/virtualenvs/orchidislandcapital.com/lib/python2.7/site-packages/django/db/models/query.py", line 854, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/Users/cole/PycharmProjects/Sites/virtualenvs/orchidislandcapital.com/lib/python2.7/site-packages/django/db/models/query.py", line 230, in iterator
    obj = model(*row_data)
  File "/Users/cole/PycharmProjects/Sites/virtualenvs/orchidislandcapital.com/lib/python2.7/site-packages/django/db/models/base.py", line 347, in __init__
    setattr(self, field.attname, val)
AttributeError: can't set attribute

Orchid的定义是class Orchid(FinancialReturnMixin, PeerPerformance)。我没有更改FinancialReturnMixin,其代码为:

class FinancialReturnMixin(models.Model):
    exclude_special_dividend = True
    round_to = 4

    shares_outstanding = models.FloatField(blank=True, null=True)
    stock_price = models.FloatField(
        verbose_name='quarter-end stock price',
        blank=True, null=True)

    class Meta:
        abstract = True
        app_label = 'snippets'

Orchid类定义的第二部分是PeerPerformance,我已经从中评论了我所做的一项更改。 PeerPerformance的定义是class PeerPerformance(DividendBookValueMixin),我在这里做的只是在模型中添加1个字段。 DividendBookValueMixin是一个抽象模型。

我删除了我的Orchid迁移,数据表和相关的south_migrationhistory条目。使用class Orchid(models.Model),兰花模型设置得很好。使用class Orchid(PeerPerformance)时,Orchid错误仍然存​​在。我对PeerPerformance的所有测试都运行了。我可以正常阅读并保存PeerPerformance个对象。

>>> from peer.models import PeerPerformance as PP
>>> pp1 = PP.objects.get(id=1)
>>> pp1.dividend = 0.135
>>> pp1.save()

DividendBookValueMixinPeerPerformance的父类。使用class Orchid(DividendBookValueMixin)时,错误仍然存​​在。我的所有测试再次DividendBookValueMixin运行。

任何想法在哪里看?

0 个答案:

没有答案