Opened 7 years ago

Closed 5 years ago

#15711 closed New feature (wontfix)

Abstract model managers should return subclass instances

Reported by: Jeremy Dunck Owned by: nobody
Component: Database layer (models, ORM) Version: 1.2
Severity: Normal Keywords: pony
Cc: Triage Stage: Design decision needed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


Suppose a base:

class Document(Model):
   title = CharField()
   class Meta:
     abstract = True

class Presentation(Document):
   num_kitten_pictures = IntegerField()

class Spreadsheet(Document):
    num_pie_charts = IntegerField()

It would be useful if Documents.objects.filter(title='x') returned instances of both Presentation and Spreadsheet whose title was 'x'.

I'd expect support only for base-class fields. This should be safe, since inheritance should follow an is-a relationship.

Change History (9)

comment:2 Changed 7 years ago by Luke Plant

Type: New feature

comment:3 Changed 7 years ago by Luke Plant

Severity: Normal

comment:4 Changed 7 years ago by Jacob

Triage Stage: UnreviewedDesign decision needed

Hm. This is starting to smell suspiciously like "narrow" support, which is a whole nest of works. Jeremy, would you expect to only support this for ABCs? Or would you want to see concrete base class support as well?

comment:5 Changed 7 years ago by anonymous

Well, I just meant for ABCs, but I see that this is a slippery slope. I think in general, concrete inheritance is to be avoided, but I understand it may be useful at times.

I suppose part of the problem is that a concrete base may be inherited by multiple subclasses so that there's no right answer in what instance to return. Diamond inheritance out of the ORM makes me queasy.

I just feel that from a reusable app perspective (and encouraging reusable apps to ship ABCs in general) it would help the app writer quite a bit to be able to rely on instance(ABC) coming out of the ORM. Is there some way we could draw a bright line around concrete to avoid narrow/widen?

comment:6 Changed 7 years ago by Jeremy Dunck

(That was me, jdunck)

comment:7 Changed 6 years ago by anonymous

Easy pickings: unset

would there be a way to use two managers for the parent model?
i.e., parent.objects and parent.subclass_objects

comment:8 Changed 6 years ago by Aymeric Augustin

UI/UX: unset

Change UI/UX from NULL to False.

comment:9 Changed 5 years ago by Aymeric Augustin

Resolution: wontfix
Status: newclosed

On one hand, this is a cool feature, and on the other hand, it's a partial implementation of STI. It has been implemented externally, see django_polymorphic.

My gut feeling is that a partial implementation will be buggy in many edge cases. To include something like this in core, we must be confident that we can handle all cases correctly, and I don't think we have enough info yet.

Note: See TracTickets for help on using tickets.
Back to Top