Model inheritance with optional self reference

16 views
Skip to first unread message

Mateusz Wroblewski

unread,
Mar 11, 2021, 5:37:20 AM3/11/21
to Django users
Hi All,

does anybody have experience with the following data model and could advise which Django tools fit it best?

Background:
I have two types of Components (Core and Complex) that share many attributes and methods, but also have their own, specific data fields and methods.

What complicates the design is the fact, that ComplexComponent might have CoreComponents assigned to it. A CoreComponent instance might be a "self-standing" entity without any relation to ComplexComponent, but there are also CoreComponents created only for the purpose of "building" ComplexComponents.
ComplexComponent (although complex :) ) might (or might not) use CoreComponents in its structure ("complexity" comes then from a different, unrelated feature).

99.99% of the time I need to be able to handle instances of CoreComponent (self-standing entities) and instances of ComplexComponent (with their linked core components) separately in different views/areas of my application, however I can envision some special use cases where I would need to use aggregation on both types to return data for some dash board type of views (e.g. average cost of all components created this year).
Also, when a ComplexComponent is handled/displayed in the view, I need to be able to access CoreComponents that "compose" it (if applicable).

I'm trying to figure out what Django tools to use for modelling this setup and would be thankful to hear your take on that problem.

My current setup uses base Component concrete model and CoreComponent/ComplexComponent deriving from it (and also being concrete models).
I end up with three different tables. Having core components and complex components in separate tables seemed right initially, but I know this approach might be difficult to handle down the road, so I'm thinking about restructuring it and using e.g. Proxy models instead? If I rebuild these models now, I would prefer to do it "right".

Does anybody have experience with a similar (the same?) design and could advise what would be the best tool in Django to implement this?

Below a simplified view of the classes in scope (only for demonstration purpose).

Many thanks for any feedback.

Have a nice day!
out.png

Kunal Solanke

unread,
Mar 11, 2021, 5:45:14 AM3/11/21
to Django users
That's too hard of a problem statement for me,ig more experienced people will be able to answer it more properly. But if its 99.99% I'd go with whatever you have right now, rather than creating bunch or proxy models. 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/36e6d27b-fd5d-4891-b999-0863a81ade85n%40googlegroups.com.

Kunal Solanke

unread,
Mar 11, 2021, 5:47:32 AM3/11/21
to Django users
Btw parts should be a m2m field ig. 

wongX Ndeso

unread,
Mar 16, 2021, 11:02:04 AM3/16/21
to django...@googlegroups.com
Just facing the same problem with you bro, and i ended up with proxy model and using django-treebeard for the data hierarchy...


Reply all
Reply to author
Forward
0 new messages