Posts Tagged ‘many-to-many’

Django on GAE using Python NDB

July 19, 2013

Recently in a comment on a post on dba.stackexchange I learned about a service called NDB, which presumably allows one to efficiently model many-to-many entity relationships in the google datastore.  And apparently it is pluggable with django, too, via this:

To use NDB with the Django web framework, add 'google.appengine.ext.ndb.django_middleware.NdbDjangoMiddleware', to the MIDDLEWARE_CLASSES entry in your Django settings.py file.

In terms of getting started with this particular kit, there is a tutorial here which I may investigate at some point.

As an additional remark, there is a comment on this post on stack overflow indicating that I’ve been a bit remiss all along, that intermediate tables are the way to store keys / manage joins if using django nonrel.  Nonetheless I think I’ll look into NDB for my future investigations, as either solution (NDB or django nonrel) requires reworking of models used in a django project, and I’d rather use one that offers automatic caching and batching, which apparently are useful things to have.

Advertisements

Blender experiments / MongoDB

April 29, 2013

Here is a picture that I made following these video tutorials ([1], [2]).

clock

I intend to start looking at these tutorials next on handling splash in blender, by the same person ([1], [2]).

Changing the subject, I’m still very muddled with the MongoDB / mySQL dichotomy, and suspect that I might need to revise my views as stated in my previous post to reflect this.  However I did find an excellent presentation on MongoDB many-to-many relationships, and how the schema model is different, here.

Essentially, this seems to be the key difference of relevance to my investigations:

  • in MongoDB there are no joins, but data is “embedded and linked”

One-to-many is arranged by referencing tables by id, eg:

blogs :
 { author : "Herge",
   date : "Sat Jul 24 2010 19:47:11 GMT-0700 (PDT)"
   comments : [
    {comment : ObjectId("1") } ]}

comments :
 { _id : "1",
   author : "James",
   date : "Sat Jul 24 2010 20:51:03 ..." }

For many-to-many, a similar idea is used, but this time, the linking goes both ways:

products :
 { _id : ObjectId("10"),
   name : "Destination Moon",
   category_ids : [ ObjectId("20"),
                    ObjectId("30")]}

categories :
  { _id : ObjectId("20"),
   name : "adventure",
   product_ids : [ ObjectId("10"),
                   ObjectId("11"),
                   ObjectId("12")]}

Then, to return all categories for a product:

db.categories.find({product_ids : ObjectId("10")})

Or all products for a given category:

db.products.find({category_ids : ObjectId("20")})

Here is an excellent post which describes more or less the same considerations, but in greater clarity and detail.

Actually, it might appear that the problem I am interested in might already have been solved, or close to.  Apparently there is a project called MongoEngine which, according to its webpage, should soon have django support (and already supports flask).  I found that page via this excellent article on how to build an elementary django application, sans admin, using said technology plus a few others.  Unfortunately MongoDB is not the same as the Google Datastore though, which is problematic for myself, although Cloud Foundry might be something to look into in said regard.  Except Cloud Foundry doesn’t support django (although micro cloud foundry, its open-sourced version, apparently does).

So it would seem for now that django-nonrel is the project of choice for use on google app engine.  One-to-many seems supported (through ListField), but not ManyToMany.  The latter would necessitate somehow allowing for db.ReferenceProperty calls via django.models.fields.related.ManyToMany ; ie rewriting that part of the package.   Or, by writing another piece of code, in DjangoToolbox, djangotoolbox.fields.ListPairField, where now the pairs kept track of are pairs of keys, following a remark in this piece on the matter of many-to-many on the google datastore.