Code

Opened 4 years ago

Closed 4 years ago

#13460 closed (duplicate)

django.db.models.Model breaks multiple inheritance

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

Description

django.db.models.Model breaks multiple inheritance by not calling

super(...).init(...)

Here's example code:

from django.db import models

class A(models.Model):
    def __init__(self, *args, **kwargs):
        super(A, self).__init__(*args, **kwargs)
        print 'A'

    m = models.CharField()

class B(object):
    def __init__(self, *args, **kwargs):
        super(B, self).__init__(*args, **kwargs)
        print 'B'

class C(object):
    def __init__(self, *args, **kwargs):
        super(C, self).__init__(*args, **kwargs)
        print 'C'

class D(A, B, C):
    def __init__(self, *args, **kwargs):
        super(D, self).__init__(*args, **kwargs)
        print 'D'

D()

Resulting in:

A
D

However as soon as I remove models.Model from parent for A and replace it with 'object', all the constructors are getting called and the output is:

C
B
A
D

Apparently this happens because MRO is nto beign composed properly when you ommit calls to parent's init, even if it's the object.
See http://fuhm.net/super-harmful/ for more details.

Attachments (0)

Change History (1)

comment:1 Changed 4 years ago by russellm

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Resolution set to duplicate
  • Status changed from new to closed

Duplicate of #13206.

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.