Skip to content

Commit

Permalink
Make it possible to use custom keyword args in model class definition
Browse files Browse the repository at this point in the history
For instance to easily customize subclasses through __init_subclass__().
  • Loading branch information
Vayel authored and vdboor committed Nov 18, 2021
1 parent 81c2d81 commit 6f44967
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions polymorphic/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ class PolymorphicModelBase(ModelBase):
PolymorphicQuerySet.
"""

def __new__(self, model_name, bases, attrs):
def __new__(self, model_name, bases, attrs, **kwargs):
# print; print '###', model_name, '- bases:', bases

# Workaround compatibility issue with six.with_metaclass() and custom Django model metaclasses:
if not attrs and model_name == "NewBase":
return super(PolymorphicModelBase, self).__new__(
self, model_name, bases, attrs
self, model_name, bases, attrs, **kwargs
)

# Make sure that manager_inheritance_from_future is set, since django-polymorphic 1.x already
Expand All @@ -78,7 +78,7 @@ def __new__(self, model_name, bases, attrs):
)

# create new model
new_class = self.call_superclass_new_method(model_name, bases, attrs)
new_class = self.call_superclass_new_method(model_name, bases, attrs, **kwargs)

# check if the model fields are all allowed
self.validate_model_fields(new_class)
Expand All @@ -100,7 +100,7 @@ def __new__(self, model_name, bases, attrs):
return new_class

@classmethod
def call_superclass_new_method(self, model_name, bases, attrs):
def call_superclass_new_method(self, model_name, bases, attrs, **kwargs):
"""call __new__ method of super class and return the newly created class.
Also work around a limitation in Django's ModelBase."""
# There seems to be a general limitation in Django's app_label handling
Expand All @@ -119,7 +119,7 @@ def call_superclass_new_method(self, model_name, bases, attrs):
if do_app_label_workaround:
meta.app_label = "poly_dummy_app_label"
new_class = super(PolymorphicModelBase, self).__new__(
self, model_name, bases, attrs
self, model_name, bases, attrs, **kwargs
)
if do_app_label_workaround:
del meta.app_label
Expand Down

0 comments on commit 6f44967

Please sign in to comment.