4

On app engine's webapp framework, I can use a polymodel to create (for example) a Goal model, and then a number of child models representing different types of goals that have various sets of fields depending on the type of goal they are. This allows me to simply query for Goal entities and receive back all the child types.

Is there any way to duplicate this using django-nonrel models? Simple model inheritance does not seem like it will work, as django-nonrel requires that the base class be abstract. Am I wrong about this, or is there another way to achieve the same affect that I'm not aware of?

Edit: One possibility that occurs to me is to go ahead and use regular django model inheritance, set the base Goal class to abstract as required and create the sub-models, and then instead of trying to query for Goal entities, creating a model for each user with a ListField that contains references to the various subclass entities and retrieving the ListField. Does this seem like a workable alternative?

1 Answers1

0

You can use ListField in djangotoolbox, it will extend django-nonrel with new type field ListField, it's just like a one-to-many type field in Django, that you can save all child entities in the field with ListProperty.

There's a useful documentation on how-to ListField: http://django-mongodb-engine.readthedocs.org/en/latest/topics/lists-and-dicts.html

Model

from djangotoolbox.fields import ListField

class Post(models.Model):
    ...
    tags = ListField()

Usage

>>> Post(tags=['django', 'mongodb'], ...).save()
>>> Post.objecs.get(...).tags
['django', 'mongodb']

Notice

For capability, you better save id instead of foreign key because you're saving different types objects into one ListField field. And better re-implement __del__ function to make sure the deletion will work well.

Colin Su
  • 4,613
  • 2
  • 19
  • 19