Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#8444 closed (invalid)

Model inheritance - inherited model could use some fields cleanup

Reported by: Robert Lujo Owned by: nobody
Component: Core (Other) Version: master
Severity: Keywords: model inheritance
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


This is not important, just a little strange (IMHO). I have following scenario:

class Question(models.Model):
class UserQuestion(Question):
class WebQuestion(Question):
>>> q = Question.objects.all()[0]
>>> dir(q)
[... 'userquestion', ..., 'webquestion'] - OK

>>> dir(q.userquestion)
[...'userquestion', ..., 'question_ptr', 'question_ptr_id', ..., 'webquestion'] 

Two issues:

  • Why to have userquestion again?
    • this looks odd:
       >>> q.userquestion.userquestion
  • Is it possible to have model instance which has multiple inheritance (one question is userquestion and webquestion in the same time)?
    • if not why to have webquestion?

Change History (3)

comment:1 Changed 11 years ago by Daniel Pope <dan@…>

Resolution: invalid
Status: newclosed
  • It has userquestion/webquestion again so that derived classes behave as an instance of the superclass (ie. polymorphism).
  • No, multiple inheritance is not possible.

Please ask for help on django-users mailing list or on the #django IRC channel, not on the tickets system.

comment:2 Changed 11 years ago by Malcolm Tredinnick

Asking support questions on the mailing is definitely to be encouraged and it should remained closed for that reason. However, in case anybody stumbles over this ticket in a search, I want to point out that both the answers in the previous comment are incorrect. There might well be some leakage of attribute names, but it's kind of impractical to avoid this because of the possibility of multiple shared parents, etc, so not worth worrying about. Some internal Python classes have similar dummy attributes. Secondly, multiple inheritance is trivially possible. There's are even some tests in Django's core tests that verify that works.

comment:3 Changed 11 years ago by Daniel Pope <dan@…>

Oh, well I'm a moron then. I didn't realise we had multiple inheritance. However the first answer is as valid as yours: we have "leakage", as you put it, because the subclass is also an instance of the superclass.

Deleting the attributes is not just impractical. It would be undesirable if isinstance(foo, Question) but foo.webquestion raised AttributeError rather than the expected WebQuestion.DoesNotExist.

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