Not sure if this will work as I don't usually do much with model
inheritance but according to docs at: 
http://docs.djangoproject.com/en/dev/topics/db/models/#id7
this might work (not pretty, but somewhat minimal code)...


p = Parent1.objects.get(id=1)
try:
   p.childa.basemthod()
except:
   try:
      p.childb.basemethod()
   except:
      raise Execption("not ChildA or ChildB")

Django adds in the lower cased name of the class as a property when
you query the super class and throws an exception if you access a
property that isn't there. So something like the above "might" work.
And obviously if you have like 5 child classes, this gets ugly very
quickly.

Dan Harris
dih0...@gmail.com


On Jun 8, 3:27 pm, John M <retireonc...@gmail.com> wrote:
> Hmm, that doesn't really get me what I want, it just caches the
> related child objects in one SQL query.
>
> What I want is a single interface and have django figure out what type
> of child object I have (like multiple inheritance but 'better').
>
> So in my example:
>
> p = Parent1.objects.get(id=1)
> p.child.basemethod()
>
> django would know what p.child is (either ChildA or ChildB) and I
> wouldn't need to know.
>
> Is that available?
>
> J
>
> On Jun 8, 11:09 am, Dejan Noveski <dr.m...@gmail.com> wrote:
>
>
>
> >http://docs.djangoproject.com/en/dev/ref/models/querysets/#id4
>
> > select_related()
>
> > On Tue, Jun 8, 2010 at 8:05 PM, John M <retireonc...@gmail.com> wrote:
> > > I've gotten model inheritance working just fine, but was hoping
> > > someone could confirm the best way to access the model.
>
> > > When I have a Base model which has a ForeignKey to a parent table, and
> > > that base model has inherited models after it, how can I refer to only
> > > one name when referencing the parent model.
>
> > > As an example:
>
> > > class Base(models.Model):
> > >    parent = models.ForeignKey(Model1)
>
> > >    class Meta:
> > >        abstract = True
>
> > >    def basemethod():
> > >      print "hello"
>
> > > class ChildA(Base):
> > >    pass
>
> > > class ChildB(Base):
> > >    pass
>
> > > p1 = Model1()
> > > p1.save
> > > x = ChildA()
> > > x.parent = P1
> > > x.save()
>
> > > p2 = Model1()
> > > p2.save()
> > > y = ChildB()
> > > y.parent = P1
> > > y.save()
>
> > > Now, I want to access the children from the parent, but I don't want
> > > to have to know what base class they are, is there a way to do that?
> > > Right now I'm using a property which figures it out based on some
> > > other fields, but would love to be able to say
>
> > > p = Parent1.objects.get(id=1)
> > > p.child.basemethod()
>
> > > Is this possible?
>
> > > Thanks
>
> > > --
> > > 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<django-users%2bunsubscr...@google
> > >  groups.com>
> > > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/django-users?hl=en.
>
> > --
> > --
> > Dejan Noveski
> > Web Developer
> > dr.m...@gmail.com
> > Twitter:http://twitter.com/dekomote|LinkedIn:http://mk.linkedin.com/in/dejannoveski

-- 
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