Skip to content

Commit 2dbcd17

Browse files
committed
Merge pull request #1 from mstachowiak/master
Fields aren't added if mixin is used more than once
2 parents 91bffdd + 85ae02e commit 2dbcd17

File tree

1 file changed

+7
-11
lines changed

1 file changed

+7
-11
lines changed

modelmixins/__init__.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,24 @@ class ModelMixin(object):
1313
"""
1414

1515
@classmethod
16-
def model_mixin(cls):
16+
def model_mixin(cls, target):
1717
"""
18-
Adds the fields of the class to the all classes that mix it in
18+
Adds the fields of the class to the target passed in.
1919
"""
20+
assert issubclass(target, models.Model)
21+
2022
fields = {}
2123

2224
for (name, attr) in cls.__dict__.items():
2325
if isinstance(attr, models.Field):
2426
fields[name] = attr
2527

26-
for fieldname in fields.keys():
27-
delattr(cls, fieldname)
28-
29-
for subclass in cls.__subclasses__():
30-
assert issubclass(subclass, models.Model)
31-
for (fieldname, field) in fields.items():
32-
field.contribute_to_class(subclass, fieldname)
33-
28+
for (key, field) in fields.items():
29+
field.contribute_to_class(target, key)
3430

3531

3632
@receiver(class_prepared)
3733
def mixin(sender, **kwargs):
3834
for base in sender.__bases__:
3935
if issubclass(base, ModelMixin):
40-
base.model_mixin()
36+
base.model_mixin(sender)

0 commit comments

Comments
 (0)