views:

128

answers:

1

if I have a Customer object with a list of orders, declared using the db.ReferenceProperty

after a while I may have huge amount of orders in there, if I pull the Customer object would I be in danger of pulling the complete set of orders?

+6  A: 

Yes, db.ReferenceProperty fields are loaded lazily. From the docs:

ReferenceProperty automatically references and dereferences model instances as property values: A model instance can be assigned to a ReferenceProperty directly, and its key will be used. The ReferenceProperty value can be used as if it were a model instance, and the datastore entity will be fetched and the model instance created when it is first used in this way. Untouched reference properties do not query for unneeded data.

So, for example:

# Any reference properties not loaded yet
customer = Customer.get_by_id(1)
print customer.name
print customer.address

# Assuming customer.order is a ReferenceProperty, now is when it
# would be loaded from the datastore.
print customer.order.created_at
Will McCutchen
May be of interest: The back-reference created by `ReferenceProperty` properties are actually `Query` instances, so you can also query for a sub-set of entities that reference the parent entity: `customer.orders.filter('company', 'ACME').order('-created').fetch(10)`
Blixt
Good point about the automagically-created queries at the other end of ReferenceProperty fields.
Will McCutchen