我的bulkloader.yaml看起来像这样:
- import: models
transformers:
- model: models.Company
connector: csv
connector_options:
encoding: utf_8
columns: from_header
property_map:
- property: __key__
external_name: companyid
import_transform: int
export_transform: transform.key_id_or_name_as_string
- property: name
external_name: name
- model: models.Home
connector: csv
connector_options:
encoding: utf_8
columns: from_header
property_map:
- property: __key__
external_name: listid
import_transform: int
export_transform: transform.key_id_or_name_as_string
- property: companyid
external_name: companyid
import_transform: transform.create_foreign_key('Company')
export_transform: transform.key_id_or_name_as_string
- property: address
external_name: address
- model: models.Bid
connector: csv
connector_options:
encoding: utf_8
columns: from_header
property_map:
- property: __key__
external_name: listid
import_transform: int
export_transform: transform.key_id_or_name_as_string
- property: listid
external_name: listid
import_transform: transform.create_foreign_key('Home',key_is_id=True)
export_transform: transform.key_id_or_name_as_string
- property: bidtext
external_name: bidtext
import_transform: db.Text
我的models.py看起来像这样
from google.appengine.ext import db
# Models
class Company(db.Model):
"""Models a real estate company"""
companyid = db.Key()
name = db.StringProperty()
class Home(db.Model):
"""Models an apartment, house, villa etc and its selling details"""
listid = db.Key()
companyid = db.ReferenceProperty(Company, collection_name='company')
address = db.StringProperty()
class Bid(db.Model):
"""Models bidding against a listid from Home model"""
listid = db.ReferenceProperty(Home, collection_name='biddings')
bidtext = db.TextProperty()
我已将数据上传到我的本地开发服务器,我可以使用http://localhost:9999/_ah/admin/datastore查看数据。因此数据上传并存在。我也看到了外键。
我无法根据父对象访问子数据。它返回空。下面粘贴的是我在本地开发数据存储区的交互式控制台中执行的内容:
from google.appengine.ext import db
from models import Home,Bid,Company
#can access parent data from child i.e. Home
bd = Bid.get_by_id(4242)
hhh = Home.get_by_id(int(bd._listid.name()))
print hhh.state
#Again, can access parent data from child i.e. Company
h = Home.get_by_id(4242)
print h.address
c = Company.get_by_id(int(h._companyid.name()))
print c.website
#Cannot find any data here (biddings is a collection name defined in Bid model)
b = Bid.gql("WHERE listid = :1", h)
for x in h.biddings:
print x.bidtext
输出:
London
Riverview 24,
http://www.houses.com/
答案 0 :(得分:0)
好吧,我发现我做错了什么。 在我的bulkloader.yaml中,我将实体称为模型而不是实物。
一旦我用过
- kind: Home
而不是
- model: models.Home
我还在外键引用上使用了 key_is_id = True 来解决查找父子数据的问题。
现在这就是我访问父母和子女数据的方式
from google.appengine.ext import db
from models import Home,Bid,Company
#get home object
h = Home.get_by_id(4242)
print h.address
#get parent values
print h.companyid.website
#get child values
b = Bid.gql("WHERE listid = :1", h.key())
for xb in b:
print xb.bidtext
我希望这有助于像我这样的一些菜鸟,因为我浪费了很多时间。 感谢所有花时间回应这个帖子的人。