From 91414c4bb825c65b0ebdcfcbaf39356befdbaf92 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Wed, 26 Jan 2011 03:42:31 +0000 Subject: [PATCH] Fixed #13206 -- call super().__init__() in Model.__init__ to allow mixins to do things there. git-svn-id: http://code.djangoproject.com/svn/django/trunk@15317 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/db/models/base.py | 2 ++ tests/modeltests/model_inheritance/models.py | 8 ++++++++ tests/modeltests/model_inheritance/tests.py | 6 +++++- 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/django/db/models/base.py b/django/db/models/base.py index 84259f1a6d..4000379c3e 100644 --- a/django/db/models/base.py +++ b/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 @@ -361,6 +362,7 @@ class Model(object): 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): diff --git a/tests/modeltests/model_inheritance/models.py b/tests/modeltests/model_inheritance/models.py index 6cee512fb3..a0fed8a5cc 100644 --- a/tests/modeltests/model_inheritance/models.py +++ b/tests/modeltests/model_inheritance/models.py @@ -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 diff --git a/tests/modeltests/model_inheritance/tests.py b/tests/modeltests/model_inheritance/tests.py index 34c136d58b..334297ae12 100644 --- a/tests/modeltests/model_inheritance/tests.py +++ b/tests/modeltests/model_inheritance/tests.py @@ -4,7 +4,7 @@ from django.core.exceptions import FieldError 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): @@ -269,3 +269,7 @@ class ModelInheritanceTests(TestCase): self.assertNumQueries(1, lambda: ItalianRestaurant.objects.select_related("chef")[0].chef ) + + def test_mixin_init(self): + m = MixinModel() + self.assertEqual(m.other_attr, 1)