-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathforms.py
28 lines (23 loc) · 989 Bytes
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
from django.forms import models
class PolymorphicModelFormMetaclass(models.ModelFormMetaclass):
def __new__(cls, name, bases, attrs):
form = super().__new__(cls, name, bases, attrs)
model = form._meta.model
form._meta.polymorphic_forms = {model: form}
if model:
for base in bases:
for mro in base.__mro__:
if issubclass(mro, PolymorphicModelForm):
mro._meta.polymorphic_forms[model] = form
return form
def __getitem__(self, model):
try:
return self._meta.polymorphic_forms[model]
except KeyError:
raise TypeError("No form registered for %s." % model)
class PolymorphicModelForm(models.ModelForm, metaclass=PolymorphicModelFormMetaclass):
def __new__(cls, *args, **kwargs):
instance = kwargs.get("instance", None)
if instance:
cls = cls[instance.__class__]
return super().__new__(cls)