Appengine JDO dataclasses to python model
Does anyone have开发者_StackOverflow tried to implement an app in GAE having both java and python?
I have an existing app and my front end is in java. Now I want to use the existing datastore to be interfaced by python. My problem is i don't know how to define the relationships and model that would be equivalent to the one in java. I have tried the one-to-many relationship in python but when stored in the datastore, the fields are different than the one-to-many of java.
My data classes are as follows. //one-to-many owned
Parent Class
public class Parent{
@PrimaryKey
@Persistent
private String unitID;
//some other fields...
@Persistent
@Order(extensions = @Extension(vendorName="datanucleus", key="list-ordering", value="dateCreated desc"))
private List <Child> child;
//methods & constructors were omitted
}
Child
public class Child{
@PrimaryKey
@Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
private Key uId;
@Persistent
private String name;
/* etc... */
}
Through some testing, I finally figured out how to do this. First is define the models or in java, it is called classes.
class Parent(db.Model):
someData = db.StringProperty(multiline=True) ....
class Child(db.Model):
someData = db.StringProperty(multiline=True) ...
Now, to set the relationship of an instance of a child to its parent, just set the parent as the ancestor of the child.
parentModel = Parent(key_name='key_of_parent')
childModel1 = Child(parent=parentModel) #set the parentModel as parent of the childModel1
childModel2 = Child(parent=parentModel) #set the parentModel as parent of the childModel2
Now you have an owned one-to-many relationship.
精彩评论