On Jan 21, 11:14 am, Aneurin Price <aneurin.pr...@gmail.com> wrote:
> (Apologies for the vague subject; I couldn't think how to summarise this :P)
>
> Hello all,
>
> I have an application where some models are inherited from others, like so:
>    Foo
>   /   \
> Bar  Baz
> Each object has (or may have) a parent object, which is a Foo (it might be a 
> Bar
> or a Baz, or some other subclass, but it's definitely a Foo). I'd like to be
> able to find the children of an object, both overall and by subclass, using
> 'foo_object.children.all()', or 'foo_object.bars.all()' and so on.
>
> Originally I set up parent links in Foo, as something like
> "parent=models.ForeignKey('self', related_name='children')", but when I 
> realised
> that I mostly want to get the children in groups, I changed the way I was 
> doing
> this. Now I have per-class links like "parent=models.ForeignKey('Foo',
> related_name='bars')", etc. This allows me to have the parent/child hierarchy
> that I want, and access the children in the appropriate groups, but it feels
> rathr ugly, and I'd still like an aggregate 'children' set so that I don't 
> have
> to use each set individually (and update every use if I ever add new
> subclasses).
>
> Additionally, I don't know if I'll need this, but I'm interested in how I 
> might
> get a link to 'children_of_same_type', so in Foo it would be an alias to
> 'children', but in Bar it would be an alias to 'bars', etc. I can't just do
> 'children_of_same_type = bars' because 'bars' is autogenerated so hasn't been
> defined yet.
>
> I hope that makes sense, but if not I can come up with a few more examples to
> try to clarify it.
>
> Does anybody have any suggestions as to how I should structure this? Would I 
> be
> best to leave the parent links as they are and define custom Managers for
> 'children' and 'children_of_same_type'? If so, pointers on how to go about 
> this
> would be appreciated.
>
> Thanks,
> Nye

Why don't you instead of subclass Foo just create a foreignkey to Foo
or a OneToOne with foo.

'foo_object.children.all()'
I don't think that this is possible because it contains different
models in the query.

The other option that you could try is a generic relation something
like the way django-tagging works.

I hope this is a help



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@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