Django Nonrel - Work on overlaying multiple tables with noSQL?

I am working on a django-nonrel project running on Google AppEngine. I want to create a model for a game that contains details that are usually common to all sports - for example, game time, status, location, etc. Then I modeled specific classes for GameBasketball, GameBaseball, etc., And they inherit from the base class.

This creates a problem, however, if I want to get something like all games on a specific day:

Game.objects.filter(gametime=mydate) 

This will return an error:

 DatabaseError: Multi-table inheritance is not supported by non-relational DBs. 

I understand that AppEngine does not support JOINs, and therefore it makes sense that this fails. But I'm not sure how to properly solve this problem in a non-relational environment. One of the solutions I tried was to turn Game into an abstract base class, and although this allows me to model the data in a good way, it still does not resolve the use case above, since it is impossible to get objects for an abstract base class.

Is this the only solution for placing all the data for all possible sports (and just leaving the fields that are not related to a particular sport) in the Game model, or is there a more elegant way to solve this problem?

EDIT: I'm more interested in understanding the correct way to handle this type of problem in any noSQL setup, and not in AppEngine. So feel free to answer, even if your answer is not specific to GAE!

+4
source share
2 answers

The reason that multiple table inheritance is not allowed in django-nerel is because the API that Django provides for such models will often use JOIN queries, as you know.

But I think that you can just manually configure the same thing that Django would do with this model inheritance โ€œsugarโ€ and just would not do the merging in your own code.

eg,

 class Game(models.Model): gametime = models.DateTimeField() # etc class GameBasketball(models.Model): game = models.OneToOneField(Game) basketball_specific_field = models.TextField() 

you will need a little extra work when you create a new GameBasketball to create the corresponding Game instance (you can try your own manager class), but after that you can at least do what you want, for example,

 qs = Game.objects.filter(gametime=mydate) qs[0].gamebasketball.basketball_specific_field 

django-nonrel and djangoappengine have a new home on GitHub: https://github.com/django-nonrel/

I'm not sure that next to the speed of the GAE datastore API itself, choosing a python framework is of great importance or that django-nonrel is inherently slower than webapp2.

0
source

For everyone who has this problem in the future, here is how I ultimately decided to solve it. Keep in mind that this is more of a solution specific to mangoes, and AFAIK there is no easy way to solve this, simply using basic non-Slack classes.

For Mongo, I can solve this problem with embedded documents (other noSQL installations may have similar functions like soft schema), simplified code below:

 class Game(models.Model): gametime = models.DateTimeField() # etc details = EmbeddedModelField() # This is the mongo specific bit. class GameBasketballDetails(models.Model): home = models.ForeignKey(Team) # etc 

Now, when I create an instance of the Game class, I save the GameBasketballDetails object in the details field. Now I can do:

 games = Game.objects.filter(gametime=mydate) games[0].details.basketball_specific_method() 

This is an acceptable solution, since it allows me to request a model of the game, but at the same time get the "child" information I need.

+1
source

All Articles