Code

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#8444 closed (invalid)

Model inheritance - inherited model could use some fields cleanup

Reported by: trebor74hr 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: UI/UX:

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 (0)

Change History (3)

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

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to invalid
  • Status changed from new to closed
  • 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 6 years ago 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.

comment:3 Changed 6 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.

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.