After adding a mixin the from is not working. Error: "ModelForm has no model class specified."
Adding a simple mixin to a ModelForm is not possible since the Meta is "overwritten" by the EntangledFormMetaclass. This is the unittest to proove. Just add at the end of tests/test_inheritance.py
class BasicFormMixin():
test_attr = "Hello"
def test_method(self):
return self.test_attr + " World"
class FormMixinTest(BasicFormMixin, ProductForm):
""" New class with mixin"""
@pytest.mark.django_db
def test_form_mixin():
# check if .Meta is handled correctly
assert hasattr(FormMixinTest.Meta, 'model')
assert FormMixinTest.Meta.model == Product
Do I miss something ? Thank you for you effort, greetings from Vienna. br Matthias
In Django actually you would access attribute _meta on a model form to find out about the model. I assume that ProductForm is from the given example.
Yes, the ProductForm is from the given example. Sorry I forgot to post the original error message from Django:
File "/home/elem/develop/gitrepos/agate/.venv/lib/python3.10/site-packages/entangled/forms.py", line 162, in __init__
super().__init__(*args, **kwargs)
File "/home/elem/develop/gitrepos/agate/.venv/lib/python3.10/site-packages/django/forms/models.py", line 350, in __init__
raise ValueError("ModelForm has no model class specified.")
ValueError: ModelForm has no model class specified.
Django is fetching the _meta in the ModelFormMetaclass from the "Meta" class see: https://github.com/django/django/blob/ea1e3703bee28bfbe4f32ceb39ad31763353b143/django/forms/models.py#L279.
In the BaseModelForm there is a check if the the model is set. see: https://github.com/django/django/blob/ea1e3703bee28bfbe4f32ceb39ad31763353b143/django/forms/models.py#L362