Tiwari
Tiwari

Reputation: 1024

App Engine, Cross reference between two entities

i will like to have two types of entities referring to each other. but python dont know about name of second entity class in the body of first yet. so how shall i code.

class Business(db.Model):
  bus_contact_info_ = db.ReferenceProperty(reference_class=Business_Info)

class Business_Info (db.Model):
  my_business_ =  db.ReferenceProperty(reference_class=Business)

if you advice to use reference in only one and use the implicitly created property (which is a query object) in other. then i question the CPU quota penalty of using query vs directly using get() on key

Pleas advise how to write this code in python

Upvotes: 1

Views: 557

Answers (1)

Robert Kluin
Robert Kluin

Reputation: 8292

Queries are a little slower, and so they do use a bit more resources. ReferenceProperty does not require reference_class. So you could always define Business like:

class Business(db.Model):
  bus_contact_info_ = db.ReferenceProperty()

There may also be better options for your datastructure too. Check out the modelling relationships article for some ideas.

Is this a one-to-one mapping? If this is a one-to-one mapping, you may be better off denormalizing your data.

Does it ever change? If not (and it is one-to-one), perhaps you could use entity groups and structure your data so that you could just directly use the keys / key names. You might be able to do this by making BusinessInfo a child of Business, then always use 'i' as the key_name. For example:

business = Business().put()
business_info = BusinessInfo(key_name='i', parent=business).put()

# Get business_info from business:
business_info = db.get(db.Key.from_path('BusinessInfo', 'i', parent=business))

# Get business from business_info:
business = db.get(business_info.parent())

Upvotes: 4

Related Questions