1

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

1 回答 1

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

我希望这对像我这样的菜鸟有所帮助,因为我在这背后浪费了很多时间。感谢所有花时间回复此主题的人。

于 2011-08-26T12:31:53.897 回答