On 05/16/2014 04:46 AM, Shai Berger wrote: > On Monday 12 May 2014 12:27:01 Thomas Güttler wrote: >> Single Table Inheritance is used by ruby-on-rails and SQLAlchemy. >> >> Are there reasons why it is used in django? >> > > Essentially, STI is a form of database denormalization. I think Django should > not encourage this.
I agree. >> I would love to see a polymorphic inheritance solution in django. >> > > Just to spell things out: You want to have models A, B(A), C(A) and D(B), so > that list(A.objects.all()) returns a list of objects of different types. > > This sounds like a good idea in general, but there are devils in the > implementation details. In particular, I'd like to separate this issue from > the issue of STI -- polymorphism is an issue of processing the retrieved > records, and need not be tightly coupled to the database layout. The > polymorphism solution should work whether the records are fetched by the > equivalent of A.objects.all() or > A.objects.select_related(child_classes).all(). > > I think you can sort-of achieve STI by doing it the other way around: Define > all your hierarchy as abstract models, with one concrete model inheriting all > of them (I suspect any STI implementation in Django would have to do > something > very similar "behind the scenes"). Pros and cons (as well as testing if this > actually works) are left as an exercise to the reader. > >> I know that there are third party apps which provide this, but >> something like this should be in the core. >> > > If you ignore STI, I think it is quite straightforward to solve this with a > parent model class which adds a type field, and manager methods to add the > select_related calls and "interpret" the type field properly; so I don't see > an > immediate need for inclusion in core. You don't even need the "type" field, you can just select_related all the subclasses and then test when iterating over the queryset which one exists for each record. This is what InheritanceManager in django-model-utils does. I don't see a need to have this in core either. It seems to me almost a perfect example of the occasionally-useful-but-not-essential functionality that is well served by third-party packages. (IMO concrete model inheritance is more often than not a questionable model-design in the first place, so Django shouldn't be adding more support around it to encourage its use.) Carl
signature.asc
Description: OpenPGP digital signature