Require setting form_fields or exclude_form_fields on ModelViewSet when not providing a get_form_class method

This matches Django's current policy on ModelForms; form_fields = '__all__' can be used to restore the old 'all fields' behaviour.
This commit is contained in:
Matt Westcott 2017-06-09 20:00:01 +01:00
parent ba4a21687a
commit 280c317c68

View file

@ -1,6 +1,7 @@
from __future__ import absolute_import, unicode_literals
from django.conf.urls import url
from django.core.exceptions import ImproperlyConfigured
from django.forms.models import modelform_factory
from wagtail.wagtailadmin.views import generic
@ -73,10 +74,20 @@ class ModelViewSet(ViewSet):
return db_field.formfield(**kwargs)
def get_form_class(self, for_update=False):
fields = getattr(self, 'form_fields', None)
exclude = getattr(self, 'exclude_form_fields', None)
if fields is None and exclude is None:
raise ImproperlyConfigured(
"Subclasses of ModelViewSet must specify 'get_form_class', 'form_fields' "
"or 'exclude_form_fields'."
)
return modelform_factory(
self.model,
formfield_callback=self.formfield_for_dbfield,
fields='__all__'
fields=fields,
exclude=exclude
)
def get_urlpatterns(self):