Hi,

you might take a look at this snippet:
http://www.djangosnippets.org/snippets/950/

Basically it's a negative of what you want (i.e. w/o subclasses) but
the idea I guess is there: you want a custom DB manager for which
SuperClass.objects returns in fact the SubClass instances. I bet there
are thousands of complications involving efficiency, filtering or
multi-inheritance but with some simplifying assumptions I guess it's
feasible.

It looks like a fundamental ORM poroblem. I've made some research but
haven't yet found a ready-to-go solution for it in Django. Please
write how it went or maybe someone else already knows how to do it
properly?

Best regards,
Mikołaj

On Nov 28, 3:45 am, Rodrigo Cea <rodrigo...@gmail.com> wrote:
> 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
>
>     defsubclass(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 callsubclass() 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 asubclass
> 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 django-us...@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.


Reply via email to