Skip to content

Commit

Permalink
[1.2.X] Fixed #13206 -- call super().__init__() in Model.__init__ to …
Browse files Browse the repository at this point in the history
…allow mixins to do things there. Backport of [15317].

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.2.X@15320 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
alex committed Jan 26, 2011
1 parent 4cee764 commit c4b0878
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
2 changes: 2 additions & 0 deletions django/db/models/base.py
@@ -1,6 +1,7 @@
import types
import sys
from itertools import izip

import django.db.models.manager # Imported to register signal handler.
from django.core.exceptions import ObjectDoesNotExist, MultipleObjectsReturned, FieldError, ValidationError, NON_FIELD_ERRORS
from django.core import validators
Expand Down Expand Up @@ -359,6 +360,7 @@ def __init__(self, *args, **kwargs):
pass
if kwargs:
raise TypeError("'%s' is an invalid keyword argument for this function" % kwargs.keys()[0])
super(Model, self).__init__()
signals.post_init.send(sender=self.__class__, instance=self)

def __repr__(self):
Expand Down
8 changes: 8 additions & 0 deletions tests/modeltests/model_inheritance/models.py
Expand Up @@ -143,3 +143,11 @@ class Copy(NamedURL):

def __unicode__(self):
return self.content

class Mixin(object):
def __init__(self):
self.other_attr = 1
super(Mixin, self).__init__()

class MixinModel(models.Model, Mixin):
pass
6 changes: 4 additions & 2 deletions tests/modeltests/model_inheritance/tests.py
Expand Up @@ -6,7 +6,7 @@
from django.test import TestCase

from models import (Chef, CommonInfo, ItalianRestaurant, ParkingLot, Place,
Post, Restaurant, Student, StudentWorker, Supplier, Worker)
Post, Restaurant, Student, StudentWorker, Supplier, Worker, MixinModel)


class ModelInheritanceTests(TestCase):
Expand Down Expand Up @@ -278,4 +278,6 @@ def test_multiple_table(self):
finally:
settings.DEBUG = old_DEBUG


def test_mixin_init(self):
m = MixinModel()
self.assertEqual(m.other_attr, 1)

0 comments on commit c4b0878

Please sign in to comment.