我的 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/