Django

Code

Ticket #8444 (closed: invalid)

Opened 4 months ago

Last modified 4 months ago

Model inheritance - inherited model could use some fields cleanup

Reported by: trebor74hr Assigned to: nobody
Milestone: Component: Core framework
Version: SVN Keywords: model inheritance
Cc: Triage Stage: Unreviewed
Has patch: 0 Needs documentation: 0
Needs tests: 0 Patch needs improvement: 0

Description

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?

Attachments

Change History

08/20/08 08:16:37 changed by Daniel Pope <dan@mauveinternet.co.uk>

  • status changed from new to closed.
  • needs_better_patch changed.
  • resolution set to invalid.
  • needs_tests changed.
  • needs_docs changed.
  • 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.

08/20/08 08:41:51 changed by mtredinnick

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.

08/20/08 08:58:57 changed by Daniel Pope <dan@mauveinternet.co.uk>

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.


Add/Change #8444 (Model inheritance - inherited model could use some fields cleanup)




Change Properties
Action