想知道如果基地在sqlalchemy繼承對象。見下面的細節。sqlalchemy Base Class提出「使用舊式的財產」錯誤
目前使用Python3和SQLAlchemy的0.8
在我的項目,我想聲明的(對象)的新樣式類,我會先行一步,並使用「屬性」在我的類中定義的屬性。類是工作好
class LoadFile(object):
def _set_year(self, value):
'''set the year'''
value = value[6:10]
if valid_integer(value) != True:
raise Exception ("File Name Error: The year value should be a numeral")
self._year = value
def _get_year(self):
'''get the year'''
return self._year
year = property(_get_year, _set_year)
「下面的示例代碼」作爲我的項目越來越多,我已經得到解決,以使用SQLAlchemy的和跑類,它扔了錯誤
class LoadFile(object):
__tablename__ = "load_file"
id = Column(Integer(10), primary_key = True)
file_name = Column(String(250), nullable = False, unique = True)
file_path = Column(String(500), nullable = False)
date_submitted = Column(DateTime, nullable = False)
submitted_by = Column(String, nullable = False)
def _set_year(self, value):
'''set the year'''
value = value[6:10]
if valid_integer(value) != True:
raise Exception ("File Name Error: The year value should be a numeral")
self._year = value
def _get_year(self):
'''get the year'''
return self._year
year = property(_get_year, _set_year)
的錯誤時扔是:
AttributeError: 'LoadFile' object has no attribute '_sa_instance_state'
File "/usr/local/lib/python3.2/dist-packages/SQLAlchemy-0.8.0b2-py3.2.egg/sqlalchemy/orm/session.py", line 1369, in add
raise exc.UnmappedInstanceError(instance)
sqlalchemy.orm.exc.UnmappedInstanceError: Class '__main__.LoadFile' is not mapped
所以我注意到,我沒有從「基地」繼承,所以我改變了我的類:
class LoadFile(Base):
因此,sqlalchemy工作正常,表已成功創建。不過,我現在發現,我得到的日食指出
Use of "property" on an old style class"
所以想知道的警告,不基地從Object繼承?以爲我早些時候會讀到它的確如此。否則,我爲什麼現在要接受這個「警告」。我知道我可以忽略它,但只是想找出確切的原因,並可能如何糾正它。
謝謝。
UPDATE
我得到了使用裝飾圍繞,如下圖所示。這樣,上述「警告」消失
@property
def year(self):
'''get the year'''
return self._year
@year.setter
def year(self, value):
'''set the year'''
if valid_integer(value) != True:
raise Exception ("File Name Error: The year value should be a numeral")
self._year = value
因此,照顧警告。但我仍然不明白爲什麼以前的方法有警告......另外,我不太確定哪個是使用裝飾器或上一個裝飾器的最佳方法。
他們實際上確實存在的向後兼容性,因此警告,而不是「停止錯誤」。無論如何,我已經證實,我在Eclipse中的Python解釋器被設置爲__「/ usr/bin/python3.2mu」__,並且我很確定正在使用python3.2 – lukik 2013-02-12 07:53:54
@lukik不,它們存在於Python 2.x中兼容性。在Python 3.x中,'class C:pass'相當於'class C(object):pass' – 2013-02-12 08:02:51