First, a little tweak. Last week, I had a problem implementing a specific methodology that I created that would allow me to manage two unique fields associated with a single db.Model object. Since this is not possible, I created a parent entity class and a class of child entities, each of which has a key name that is assigned one of the unique values. You can find my previous question located here , which includes my sample code and a general explanation of my insert process.
In my initial question, someone commented that my solution would not solve my problem of having to use two unique fields associated with a single db.Model object.
My implementation tried to solve this problem by implementing a static method that creates ParentEntity, and its key_name property is assigned to one of my unique values. In the second step of my process, I create a child and assign the parent to the parent parameter. Both of these steps are performed in the db transaction, so I assumed that this would make the uniqueness problem work, as both of my values were stored in two separate key_name fields on two separate models.
The comment noted that this solution would not work because when you set the parent to the child, the key name is no longer unique to the entire model, but instead unique to the records of the parent and child. Bummer ...
I believe that I could solve this new problem by changing how these two models relate to each other.
, . , key_name. , . , . "--", , , .
- . GAE Datastore db , . , .
. ? , , . , . ? , , .
, . .
class ParentEntity(db.Model):
str1_key = db.StringProperty()
str2 = db.StringProperty()
@staticmethod
def InsertData(string1, string2, string3):
try:
def txn():
prt = ParentEntity(
key_name=string1,
str1_key=string1,
str2=string2)
prt.put()
child = ChildEntity(
key_name=string2,
parentEnt=prt,
str1=string1,
str2_key=string2,
str3=string3,)
child.put()
return child
db.run_in_transaction(txn)
except Exception, e:
raise e
class ChildEntity(db.Model):
str1 = db.StringProperty()
str2_key = db.StringProperty()
parentEnt = db.ReferenceProperty(reference_class=ParentEntity)
str3 = db.StringProperty()