基于模型的类的继承,Python
我目前有一个使用以下代码的项目: 类.py基于模型的类的继承,Python,python,Python,我目前有一个使用以下代码的项目: 类.py class Session(Conference, ndb.Model): ''' Session - A Session object ''' # The session's name sessionName = ndb.StringProperty() # Highlights of the session highlights = ndb.StringProperty(repeated =
class Session(Conference, ndb.Model):
''' Session - A Session object '''
# The session's name
sessionName = ndb.StringProperty()
# Highlights of the session
highlights = ndb.StringProperty(repeated = True)
# The session's speaker
speaker = ndb.StringProperty()
# How long the session will last
duration = ndb.TimeProperty()
# The type of session (Workshop, lecture, etc...); Default 'Not Specified'
typeOfSession = ndb.StringProperty(default = 'NOT_SPECIFIED')
# Date the session takes place on
date = ndb.DateProperty()
# The time the session starts
startTime = ndb.TimeProperty()
# The Conference this Session is at
parentConf = Conference
class Conference(ndb.Model):
'''Conference -- Conference object'''
name = ndb.StringProperty(required=True)
description = ndb.StringProperty()
organizerUserId = ndb.StringProperty()
topics = ndb.StringProperty(repeated=True)
city = ndb.StringProperty()
startDate = ndb.DateProperty()
month = ndb.IntegerProperty() # TODO: do we need for indexing like Java?
endDate = ndb.DateProperty()
maxAttendees = ndb.IntegerProperty()
seatsAvailable = ndb.IntegerProperty()
我正试图调用代码
conf=会议(**数据).put()
变量=会话(conf,**数据).put()
其中conf是一个会议对象(我已经验证了这一点)
**data是一个输入数据数组,它可以工作(我已经测试过了)
但是当我调用这个函数时,我得到了一个错误
“TypeError:模型构造函数不接受位置参数。”
经过一些搜索,我发现这是由于我试图重载ndb.Model的构造函数,我被告知我不应该这样做
所以我的问题是,我将如何管理这种继承,其中Session是Conference的子对象,但在构建过程中仍然使用ndb.Model数据?谢谢你的时间 将PolyModel用于数据存储继承-> 以下是一个例子:
from google.appengine.ext import ndb
from google.appengine.ext.ndb import polymodel
class Contact(polymodel.PolyModel):
phone_number = ndb.PhoneNumberProperty()
address = ndb.PostalAddressProperty()
class Person(Contact):
first_name = ndb.StringProperty()
last_name = ndb.StringProperty()
mobile_number = ndb.PhoneNumberProperty()
class Company(Contact):
name = ndb.StringProperty()
fax_number = ndb.PhoneNumberProperty()
p = Person(phone_number='1-206-555-9234',
address='123 First Ave., Seattle, WA, 98101',
first_name='Alfred',
last_name='Smith',
mobile_number='1-206-555-0117')
p.put()
c = Company(phone_number='1-503-555-9123',
address='P.O. Box 98765, Salem, OR, 97301',
name='Data Solutions, LLC',
fax_number='1-503-555-6622')
c.put()
for contact in Contact.query():
print 'Phone: %s\nAddress: %s\n\n' % (contact.phone_number, contact.address)
将PolyModel用于数据存储继承-> 以下是一个例子:
from google.appengine.ext import ndb
from google.appengine.ext.ndb import polymodel
class Contact(polymodel.PolyModel):
phone_number = ndb.PhoneNumberProperty()
address = ndb.PostalAddressProperty()
class Person(Contact):
first_name = ndb.StringProperty()
last_name = ndb.StringProperty()
mobile_number = ndb.PhoneNumberProperty()
class Company(Contact):
name = ndb.StringProperty()
fax_number = ndb.PhoneNumberProperty()
p = Person(phone_number='1-206-555-9234',
address='123 First Ave., Seattle, WA, 98101',
first_name='Alfred',
last_name='Smith',
mobile_number='1-206-555-0117')
p.put()
c = Company(phone_number='1-503-555-9123',
address='P.O. Box 98765, Salem, OR, 97301',
name='Data Solutions, LLC',
fax_number='1-503-555-6622')
c.put()
for contact in Contact.query():
print 'Phone: %s\nAddress: %s\n\n' % (contact.phone_number, contact.address)