tags:

views:

106

answers:

4

I have defined some models which look like this:

class ABClass(models.Model):
   #common attributes
   class Meta:
     abstract = True

class ConcreteClass1(ABClass):
   #implementation specific attributes

class ConcreteClass2(ABClass):
   #implementation specific attributes

class ModifiedConcreteClass1(ConcreteClass1):
   #implementation specific attributes

I have a view which takes a 'type' parameter which specifies the type of class to display. If 'type=None' I want to display all descendents of ABClass (in this case, ConcreteClass{1,2}, ModifiedConcreteClass1). I have poked around the documentation and cannot seem to find a way of doing this which does not require list of ConcreteClasses be maintained. ABClass.__subclasses__() seemed promising but it returns an empty list when I call it.

At present the best strategy I have is to create a CLASS_LIST variable in models.py which is populated with all descendents of ABClass at runtime using inspect methods. Is there a way to do this using the django api?

Thanks

+1  A: 

I was able to use the __subclass__() method but not on an abtract=True class. Removing the abstract=True will add another table to your database (and more overhead, I would guess) but but then you can get all its subclasses as you described.

thornomad
A nice work around, but I definitely would like to keep the abstract=True to avoid another table being created.
pisswillis
+2  A: 

I haven't look at what Django does when you set abstract True in the backend, but I played with this and I found that this works. Please note, it only works in Python 2.6

from abc import ABCMeta

class ABClass():
    __metaclass__ = ABCMeta

class ConcreteClass1(ABClass):
     pass

class ConcreteClass2(ABClass):
     pass

print ABClass.__subclasses__()

Results in

[<class '__main__.ConcreteClass1'>, <class '__main__.ConcreteClass2'>]

Without using the ABCMeta and __metaclass__, you will receive an empty list.

You can read a good description of what's going on here. Only problem with this, and I'm not sure if it will affect you is I can't quite figure out why when I create an instance of ABClass, it can't find the subclasses. Perhaps by playing around a bit and reading the doc more it might get you somewhere.

Let me know how this works for you, as I am genuinely curious on the true answer.

Bartek
This doesn't really work with django's ORM.
Paul McMillan
What does that have to do with the ORM? Looks like he just wanted to get the subclasses.
Bartek
+1  A: 

I realize that this is kind of an ugly solution, but I've done this before:

class ABClass(models.Model):
   #common attributes
   is_ABClass = True
   class Meta:
     abstract = True

class ConcreteClass1(ABClass):
   #implementation specific attributes

class ConcreteClass2(ABClass):
   #implementation specific attributes

class ModifiedConcreteClass1(ConcreteClass1):
   #implementation specific attributes

def get_ABClasses():
   this = modules[__name__]
   return [getattr(this, attr) for attr in dir(this)
           if hasattr(getattr(this, attr), 'is_ABClass') and attr != 'ABClass']
Jeff
+2  A: 

Take a look at this similar question:

http://stackoverflow.com/questions/929029/how-do-i-access-the-child-classes-of-an-object-in-django-without-knowing-the-name

My solution to that was essentially this:

def get_children(self):
    rel_objs = self._meta.get_all_related_objects()
    return [getattr(self, x.get_accessor_name()) for x in rel_objs if x.model != type(self)]

Though there are a number of other good solutions there as well. It seems likely that you actually want the children of the base class, and not just the child classes. If not, the answers there may serve as a motivator for your solution.

Paul McMillan