You need to create the parent's key, but you don't need to create a
parent entity in the datastore.

You can use "k = models.Parent(key_name='parent_key')" to create a
parent node and then simply use k as you've been using it (without the
put or get).  (When you specify a key-name, the model instance has a
key even though it hasn't been put.)

You can also create the parent key directly with "parent_key =
db.Key.from_path(models.Parent.kind(), 'parent_key')" and use
parent_key where you've been using k.  (The documentation for db.Model
says that the value of parent can be an instance that has a valid key
or a key.)

Yes, ancestor queries with such keys works even though there's no
corresponding entity in the datastore.

On Nov 11, 1:31 pm, Simo Salminen <[email protected]> wrote:
> I have database entities that I want to change in transaction. For
> this I have to create an entity group. However, the entities don't
> have any natural parent entity, so I have created a dummy parent
> entity. Is there another good way to do this?
>
> Currently I am using the system below. First, I have following data
> model.
>
> class Event(db.Model):
>     date = db.DateTimeProperty(required=True)
>     name = db.StringProperty(required=True)
>
> I have created a "dummy" parent:
> class Parent(db.Model):
>     pass
>
> Creating parent and defining the parent for the entity (this code is
> inside the transaction):
> try:
>         k = db.get("parent_key")
> except db.BadKeyError:
>         k = models.Parent(key_name="parent_key")
>         k.put()
>
> ancestor_key = k.key()
> q = db.GqlQuery('SELECT * WHERE ANCESTOR IS :1', ancestor_key)
> # (using the query results here, code clipped)
> e = models.Event(date=date, name=name, parent=k)
> e.put()
>
> Can I avoid creating this dummy "Parent" model, and satisfy the
> "entity group/transaction" requirement in more elegant way?

--

You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=.


Reply via email to