You do this by creating a Category Class, with a string property Name, an
optional reference property ParentCategory, and two functions:
1. CategoryList() which recursively returns a list of categories including
the category and all child categories,
2. TopCategory() which recurses upwards and returns the top most parent
category. (You need this to validate any ParentCollection selection, you
don't want to set a Category's parent to be equal to any Category who's top
most parent is the same as the category in question, or you define an
endless loop that will crash your CategoryList().
All of your items must have a required reference property Category. You
search on Item.Category IN Category.CategoryList().
You can add n-levels of categories to the Category Table.
LivingThing = Category (Name = "Living Thing")
Animal = Category (Name = "Animal", ParentCategory = LivingThing)
Mammal = Category (Name = "Mammal", ParentCategory = Animal)
Plant = Category(Name = "Plant", ParentCategory = LivingThing)
LivingThing.CategoryList() # returns {LivingThing, Animal,Mammal,Plant}
Animal.CategoryList() # returns {Animal, Mammal}
Plant.CategoryList() # returns {Plant}
If you later decide you want to store inanimate objects, you could then add
InAnimateObject = Category (Name "Inanimate Objects")
ManMade = Category(Name="Inanimate Objects", InAnimateObject)
In which case you would have two top level categories, LivingThing, and
InAnimateObject. You will need to validate any ParentCategory Selection
that the user makes to prevent a circular heirarchy. (E.G. if you set the
parent of LivingThing to Mammal, then the CategoryList() of LivingThing,
Animal, and Mammal would recurse forever. This is why you need
Category.TopCategoy() -> ThisCategory.ParentCategory.TopCategory() Can't be
equal to ThisCategory.
As a variation of this, you could make the Category Class contain a
reference LIST property ParentCategories. You would do this if you wanted
to create a heirarchy that was more robust, which would allow you to add,
forexample:
ClonedAnimal = Category(Name ="Cloned Animal", ParentCategories = {Animal,
ManMade})
Sheep = Category(Name = "Sheep", ParentCategories = {Animal})
ClonedSheep = Category (Name="ClonedSheep", ParentCategories= {Sheep,
ClonedAnimal})
Then
Sheep.CategoryList() # returns {Sheep, ClonedSheep}
ClonedAnimal.CategoryList() # returns {ClonedAnimal, ClonedSheep}
ManMade.CategoryList() # returns {ManMade, ClonedAnimal, ClonedSheep}
On Mon, Jul 19, 2010 at 4:59 PM, Bill Edwards <[email protected]> wrote:
> Hi guys,
>
> Thanks for the suggestions! However, there is a slight detail that I
> forgot to mention. I need to be able to dynamically define the
> subcategories, and I have literally hundreds of them already defined
> (more to be added by users). Storing the categories as a
> StringListProperty seems promising. That means that when I'm writing
> an entity to the datastore with a specific supercategory, I have to
> look for all subcategories and add those to the list as well. Can you
> guys suggest a schema that won't require an extra call to the database
> by storing the relationships within the schema?
>
> Thanks!
>
> On Jul 19, 2:20 am, gops <[email protected]> wrote:
> > you can achieve this in polymodel by adding all common properties to
> > polymodel base class and specific property to the hierarchy.
> >
> > i.e.
> >
> > Class LivingThing(polymodel.PolyModel):
> > Kingdom = db.StringProperty()
> > Phylum = db.StringProperty()
> > Subphylum = db.StringProperty()
> >
> > Class Mammal(LivingThing):
> > noofbreast = db.IntegerProperty()
> >
> > class Monkey(Mammal):
> > canjump = db.BooleanProperty()
> >
> > now you can create class monkey as,
> > Monkey(Kingdom="Animal",canjump=True)
> >
> > and later query as LivingThing.filter("Kingdom =","Animal").fetch()
> >
> > can return Monkey.
> >
> > On Jul 19, 1:04 pm, Bill Edwards <[email protected]> wrote:
> >
> > > Hi!
> >
> > > I've been recently trying to wrap my head around GAE's datatypes. Is
> > > there a model type in GAE that allows me to define subproperties? For
> > > example, I want to have a "category" property where some categories
> > > fall under others, and when i query for all entities within a
> > > supercategory, i get the subcategories also.
> >
> > > For example:
> > > class Animals(db.Model):
> > > type = db.StringProperty()
> >
> > > Animals(type='mammal')
> > > Animals(type='chimpanzee')
> > > Animals(type='monkey')
> >
> > > When I query for all animals with type mammal, i want to return all 3
> > > of the above objects.
> >
> > > I've read through the documentation, and the PolyModel datastore model
> > > has struck my eye as being potentially helpful. However, it doesn't
> > > seem to quite do the job.
> >
> > > Thanks!
>
> --
> 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]<google-appengine%[email protected]>
> .
> For more options, visit this group at
> http://groups.google.com/group/google-appengine?hl=en.
>
>
--
Michael S. DeLaney
[email protected]
DeLaney Law Offices, Ltd.
ELDER LAW ATTORNEYS
14524 John Humphrey Drive
Orland Park, IL 60462
(708) 675-7144
(708) 675-7150 - Direct Dial
Fax: (866) 282-6509
NOTICE: The information contained in this electronic mail transmission
(including any attachment) is intended by DeLaney Law Offices, Ltd. for the
use of the named individual or entity to which it is directed and may
contain information that it is privileged or otherwise confidential. It is
not intended for transmission to, or receipt by, anyone other than the named
addressee (or a person authorized to deliver it to the named addressee). It
should not be copied or forwarded to any unauthorized persons. If you have
received this electronic mail transmission in error, please delete it from
your system without copying or forwarding it, and notify the sender of the
error by reply e-mail or by calling DeLaney Law Offices, Ltd. at
708-675-7144 so that our address record may be corrected. Thank you.
IRS CIRCULAR 230 NOTICE. Any advice expressed above as to tax matters was
neither written nor intended by the sender or DeLaney Law Offices, Ltd. to
be used and cannot be used by any taxpayer for the purpose of avoiding tax
penalties that may be imposed under U.S. tax law. If any person uses or
refers to any such tax advice in promoting, marketing or recommending a
partnership or other entity, investment plan or arrangement to any taxpayer,
then (i) the advice was written to support the promotion or marketing (by a
person other than DeLaney Law Offices, Ltd.) of that transaction or matter,
and (ii) such taxpayer should seek advice based on the taxpayer's particular
circumstances from an independent tax advisor.
--
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=en.