Reputation: 5605
I am somewhat new to the google app engine models system(and all database models for that matter), and I am trying to figure out how make a model have a collection of model instances as one of the properties. Some how I feel that there has to be a better way to model the data I have.
So here is the example. I'm writing a journal webapp. I want to allow users to have multible journals as defined by the model Journals
#Represents a collection of journals
class Journals(db.Model):
user = db.UserProperty()
journals = db.ListProperty(int) #there has to be a better way of keeping track of journals then by a list of their id
In the property journals
I keep a list of the id's of the Journal
model(shown below).
class Journal(db.Model):
user = db.UserProperty()
name = db.StringProperty()
date_created = db.DateTimeProperty(auto_now_add=True)
last_modified = db.DateTimeProperty(auto_now=True)
journal_item = db.ListProperty(int)
In the Journal model I store a list of id's of JournalItem
instances as a list of id's
class JournalItem(db.Model):
user = db.UserProperty()
name = db.StringProperty()
date_created = db.DateTimeProperty(auto_now_add=True)
last_modified = db.DateTimeProperty(auto_now=True)
content = db.StringProperty(multiline=True)
So I guess the question really is how should I model data with this structure(I omitted some properties to simplify it)(shown in json).
"Journals": {
"Journal": {
"user": "GAE user object",
"name": "journal 1",
"JournalItems": {
"JournalItem": {
"name": "entry 1",
"content": "some example content"
},
"JournalItem": {
"name": "entry 2",
"content": "some example content"
},
"JournalItem": {
"name": "entry n",
"content": "some example content"
}
}
}
"Journal": {
"user": "GAE user object",
"name": "journal 2",
"JournalItems": {
"JournalItem": {
"name": "entry 1",
"content": "some example content"
},
"JournalItem": {
"name": "entry 2",
"content": "some example content"
},
"JournalItem": {
"name": "entry n",
"content": "some example content"
}
}
}
}
I hope that wasn't to long-winded to be annoying. Any help on the matter would be greatly appreciated.
Upvotes: 1
Views: 322
Reputation: 8221
You can follow this guide which describes in an elegant fashion how you can model your entities relationships on Google Appengine.
The reversed approach you can take is to create a reference for example of a Journal in the JournalItem entity and assign a collection_name to it.
class JournalItem:
Journal = db.ReferencePoperty(Journal, collection_name='journal_items')
name = db....
content = db...
The you will be able to iterate through the JournalItems of the Journal in this fashion:
for item in journal.journal_items
logging.info(item.content)
Upvotes: 1
Reputation: 599490
That's pretty much the way to do it, except I would use a ListProperty(db.Key)
instead of int. Then you can do db.get(userjournals.journals)
to get all the user's journals once you have the Journals
instance belonging to that user (note that to avoid confusion, you should probably call that something like UserJournals
).
Remember that GAE is a non-relational datastore, and sometimes you have to think a bit differently from how you would using SQL.
Upvotes: 3