Opened 13 years ago

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

Description

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 by Luke Plant, 13 years ago

Type: New feature

comment:3 by Luke Plant, 13 years ago

Severity: Normal

comment:4 by Jacob, 13 years ago

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 by anonymous, 13 years ago

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 by Jeremy Dunck, 13 years ago

(That was me, jdunck)

comment:7 by anonymous, 13 years ago

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 by Aymeric Augustin, 12 years ago

UI/UX: unset

Change UI/UX from NULL to False.

comment:9 by Aymeric Augustin, 11 years ago

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