运行此代码时出现上述错误。当我返回时会发生这种情况return modelformset_factory
,def get_form_class(self):
但是当我使用return modelform_factory
. 代码可能有什么问题?谢谢
class ModelAdminMixin(LoginRequiredMixin, PermissionMixin):
model = None
#--------------------------------------------------------------------------
def get_form(self, form_class):
form_class = self.get_form_class()
form_kwargs = self.get_form_kwargs()
if hasattr(self, 'request'):
request = self.request
form_kwargs['request'] = request
return form_class(**form_kwargs)
#--------------------------------------------------------------------------
def get_form_class(self):
# if the View sent has it's own form - use it
if getattr(self, 'form_class', None) is not None:
return self.form_class
# check the global form_override settings
form = None
if self.get_model() in form_mapping:#There is a form_mapping.py file
# format is Model: Form
form = form_mapping[self.get_model()]
# if no overriding found - use the standard
if form is None:
form = form_mapping['default']
# create model form dynamically
return modelformset_factory(self.get_model(), extra=2)
#--------------------------------------------------------------------------
def get_model(self):
return self.model
def get_queryset(self):
""" Default queryset is all model's instances
"""
return self.get_model()._default_manager.all()
#--------------------------------------------------------------------------
def get_context_data(self, **kwargs):
""" Inject models into context
"""
context = super(ModelAdminMixin, self).get_context_data(**kwargs)
context['model'] = self.get_model()
context['model_str'] = str(self.get_model())
return context
#--------------------------------------------------------------------------
def dispatch(self, request, *args, **kwargs):
""" Dynamic model import
"""
app = self.kwargs.get('app')
model = self.kwargs.get('model')
if self.model is None and app and model:
app = __import__(app)
app_models = getattr(app, 'models')
model = getattr(app_models, model)
self.model = model
return super(ModelAdminMixin, self).dispatch(request, *args, **kwargs)
追溯
Traceback:
File "/path/to/django/environments/env1.5/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/path/to/django/environments/env1.5/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/path/to/nexted/nexted/admin/mixins.py" in dispatch
187. return super(ModelAdminMixin, self).dispatch(request, *args, **kwargs)
File "/path/to/nexted/nexted/admin/mixins.py" in dispatch
113. *args, **kwargs)
File "/path/to/nexted/nexted/admin/mixins.py" in dispatch
63. return super(PermissionMixin, self).dispatch(request, *args, **kwargs)
File "/path/to/django/environments/env1.5/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
86. return handler(request, *args, **kwargs)
File "/path/to/django/environments/env1.5/local/lib/python2.7/site-packages/django/views/generic/edit.py" in get
195. return super(BaseCreateView, self).get(request, *args, **kwargs)
File "/path/to/django/environments/env1.5/local/lib/python2.7/site-packages/django/views/generic/edit.py" in get
154. form = self.get_form(form_class)
File "/path/to/django/projects/nexted/nexted/admin/mixins.py" in get_form
135. return form_class(**form_kwargs)
File "/path/to/django/environments/env1.5/local/lib/python2.7/site-packages/django/forms/models.py" in __init__
441. super(BaseModelFormSet, self).__init__(**defaults)
Exception Type: TypeError at /admin/sch/Sch/create/
Exception Value: __init__() got an unexpected keyword argument 'instance'