注意:我終於找到了錯誤,所以下面的文本或許只對我有用。簡短的回答:我決定從我之前定義爲@property-method
的屬性中創建一個模型字段。我沒有刪除@property-method
唯一的地方是在蘭花模型。Django:調試AttributeError:無法設置屬性
經過一番調整和戳我的代碼,我突然得到這個錯誤: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
定義爲蘭花是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)
蘭花錯誤仍然存在。我所有的測試都針對PeerPerformance
運行。我可以讀取並保存PeerPerformance
對象。
>>> from peer.models import PeerPerformance as PP
>>> pp1 = PP.objects.get(id=1)
>>> pp1.dividend = 0.135
>>> pp1.save()
DividendBookValueMixin
是PeerPerformance
父類。用class Orchid(DividendBookValueMixin)
錯誤依然存在。我所有的測試再次DividendBookValueMixin
運行。
任何想法在哪裏看?