I have a parent Class "ComponentBase", with abstract=False, that many
other classes extend.
The parent Class has a ForeignKey relation to another class, "Band".
It is convenient to be able to get a list of all of a band's
components without having to explicitly list each component class.
I am doing this currently like so:
class ComponentBase(models.Model):
band = models.ForeignKey(Band)
...
class Meta:
abstract = False
def subclass(self):
try: return self.complogo
except: pass
try: return self.compnota
except: pass
try: return self.compalbumfotos
except: pass
...
where Complogo, Compnota, etc., are ComponentBase's sublclasses, like
so:
class CompLogo(ComponentBase):
....
. With this, I can take an instance of Band and do:
components = ComponentBase.objects.filter(band=band_instance)
which populates "components" with all of the band's ComponentBase
instances, and then for each one call subclass() to retrieve the
actual component, e.g. Complogo().
The thing is the number of components will likely increase in the
future, and it feels messy to keep extending the try/except list. Is
there a more compact and less verbose way of retrieving a subclass
instance when all you have is its parent class instance?
--
You received this message because you are subscribed to the Google Groups
"Django users" 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/django-users?hl=en.