Python django.forms.models.modelform_factory() Examples
The following are 30
code examples of django.forms.models.modelform_factory().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
django.forms.models
, or try the search function
.
Example #1
Source File: detail.py From StormOnline with Apache License 2.0 | 7 votes |
def get_model_form(self, **kwargs): """ Returns a Form class for use in the admin add view. This is used by add_view and change_view. """ if self.exclude is None: exclude = [] else: exclude = list(self.exclude) if self.exclude is None and hasattr(self.form, '_meta') and self.form._meta.exclude: # Take the custom ModelForm's Meta.exclude into account only if the # ModelAdmin doesn't define its own. exclude.extend(self.form._meta.exclude) # if exclude is an empty list we pass None to be consistant with the # default on modelform_factory exclude = exclude or None defaults = { "form": self.form, "fields": self.fields and list(self.fields) or '__all__', "exclude": exclude, } defaults.update(kwargs) return modelform_factory(self.model, **defaults)
Example #2
Source File: editable.py From weibo-analysis-system with MIT License | 6 votes |
def get(self, request, object_id): model_fields = [f.name for f in self.opts.fields] fields = [f for f in request.GET['fields'].split(',') if f in model_fields] defaults = { "form": self.form, "fields": fields, "formfield_callback": self.formfield_for_dbfield, } form_class = modelform_factory(self.model, **defaults) form = form_class(instance=self.org_obj) helper = FormHelper() helper.form_tag = False helper.include_media = False form.helper = helper s = '{% load i18n crispy_forms_tags %}<form method="post" action="{{action_url}}">{% crispy form %}' + \ '<button type="submit" class="btn btn-success btn-block btn-sm">{% trans "Apply" %}</button></form>' t = template.Template(s) c = template.Context({'form': form, 'action_url': self.model_admin_url('patch', self.org_obj.pk)}) return HttpResponse(t.render(c))
Example #3
Source File: detail.py From imoocc with GNU General Public License v2.0 | 6 votes |
def get_model_form(self, **kwargs): """ Returns a Form class for use in the admin add view. This is used by add_view and change_view. """ if self.exclude is None: exclude = [] else: exclude = list(self.exclude) if self.exclude is None and hasattr(self.form, '_meta') and self.form._meta.exclude: # Take the custom ModelForm's Meta.exclude into account only if the # ModelAdmin doesn't define its own. exclude.extend(self.form._meta.exclude) # if exclude is an empty list we pass None to be consistant with the # default on modelform_factory exclude = exclude or None defaults = { "form": self.form, "fields": self.fields and list(self.fields) or '__all__', "exclude": exclude, } defaults.update(kwargs) return modelform_factory(self.model, **defaults)
Example #4
Source File: detail.py From online with GNU Affero General Public License v3.0 | 6 votes |
def get_model_form(self, **kwargs): """ Returns a Form class for use in the admin add view. This is used by add_view and change_view. """ if self.exclude is None: exclude = [] else: exclude = list(self.exclude) if self.exclude is None and hasattr(self.form, '_meta') and self.form._meta.exclude: # Take the custom ModelForm's Meta.exclude into account only if the # ModelAdmin doesn't define its own. exclude.extend(self.form._meta.exclude) # if exclude is an empty list we pass None to be consistant with the # default on modelform_factory exclude = exclude or None defaults = { "form": self.form, "fields": self.fields and list(self.fields) or '__all__', "exclude": exclude, } defaults.update(kwargs) return modelform_factory(self.model, **defaults)
Example #5
Source File: editable.py From StormOnline with Apache License 2.0 | 6 votes |
def get(self, request, object_id): model_fields = [f.name for f in self.opts.fields] fields = [f for f in request.GET['fields'].split(',') if f in model_fields] defaults = { "form": self.form, "fields": fields, "formfield_callback": self.formfield_for_dbfield, } form_class = modelform_factory(self.model, **defaults) form = form_class(instance=self.org_obj) helper = FormHelper() helper.form_tag = False helper.include_media = False form.helper = helper s = '{% load i18n crispy_forms_tags %}<form method="post" action="{{action_url}}">{% crispy form %}' + \ '<button type="submit" class="btn btn-success btn-block btn-sm">{% trans "Apply" %}</button></form>' t = template.Template(s) c = template.Context({'form': form, 'action_url': self.model_admin_url('patch', self.org_obj.pk)}) return HttpResponse(t.render(c))
Example #6
Source File: detail.py From weibo-analysis-system with MIT License | 6 votes |
def get_model_form(self, **kwargs): """ Returns a Form class for use in the admin add view. This is used by add_view and change_view. """ if self.exclude is None: exclude = [] else: exclude = list(self.exclude) if self.exclude is None and hasattr(self.form, '_meta') and self.form._meta.exclude: # Take the custom ModelForm's Meta.exclude into account only if the # ModelAdmin doesn't define its own. exclude.extend(self.form._meta.exclude) # if exclude is an empty list we pass None to be consistant with the # default on modelform_factory exclude = exclude or None defaults = { "form": self.form, "fields": self.fields and list(self.fields) or '__all__', "exclude": exclude, } defaults.update(kwargs) return modelform_factory(self.model, **defaults)
Example #7
Source File: editable.py From StormOnline with Apache License 2.0 | 6 votes |
def post(self, request, object_id): model_fields = [f.name for f in self.opts.fields] fields = [f for f in request.POST.keys() if f in model_fields] defaults = { "form": self.form, "fields": fields, "formfield_callback": self.formfield_for_dbfield, } form_class = modelform_factory(self.model, **defaults) form = form_class( instance=self.org_obj, data=request.POST, files=request.FILES) result = {} if form.is_valid(): form.save(commit=True) result['result'] = 'success' result['new_data'] = form.cleaned_data result['new_html'] = dict( [(f, self.get_new_field_html(f)) for f in fields]) else: result['result'] = 'error' result['errors'] = JsonErrorDict(form.errors, form).as_json() return self.render_response(result)
Example #8
Source File: authentication.py From Wooey with BSD 3-Clause "New" or "Revised" License | 6 votes |
def wooey_login(request): if wooey_settings.WOOEY_AUTH == False: return HttpResponseRedirect(wooey_settings.WOOEY_LOGIN_URL) User = get_user_model() form = modelform_factory(User, fields=('username', 'password')) user = User.objects.filter(username=request.POST.get('username')) if user: user = user[0] else: user = None form = form(request.POST, instance=user) if form.is_valid(): data = form.cleaned_data user = authenticate(username=data['username'], password=data['password']) if user is None: return JsonResponse({'valid': False, 'errors': {'__all__': [force_text(_('You have entered an invalid username or password.'))]}}) login(request, user) return JsonResponse({'valid': True, 'redirect': request.POST['next']}) else: return JsonResponse({'valid': False, 'errors': form.errors})
Example #9
Source File: detail.py From devops with MIT License | 6 votes |
def get_model_form(self, **kwargs): """ Returns a Form class for use in the admin add view. This is used by add_view and change_view. """ if self.exclude is None: exclude = [] else: exclude = list(self.exclude) if self.exclude is None and hasattr(self.form, '_meta') and self.form._meta.exclude: # Take the custom ModelForm's Meta.exclude into account only if the # ModelAdmin doesn't define its own. exclude.extend(self.form._meta.exclude) # if exclude is an empty list we pass None to be consistant with the # default on modelform_factory exclude = exclude or None defaults = { "form": self.form, "fields": self.fields and list(self.fields) or None, "exclude": exclude, } defaults.update(kwargs) return modelform_factory(self.model, **defaults)
Example #10
Source File: editable.py From devops with MIT License | 6 votes |
def get(self, request, object_id): model_fields = [f.name for f in self.opts.fields] fields = [f for f in request.GET['fields'].split(',') if f in model_fields] defaults = { "form": self.form, "fields": fields, "formfield_callback": self.formfield_for_dbfield, } form_class = modelform_factory(self.model, **defaults) form = form_class(instance=self.org_obj) helper = FormHelper() helper.form_tag = False form.helper = helper s = '{% load i18n crispy_forms_tags %}<form method="post" action="{{action_url}}">{% crispy form %}' + \ '<button type="submit" class="btn btn-success btn-block btn-sm">{% trans "Apply" %}</button></form>' t = template.Template(s) c = template.Context({'form': form, 'action_url': self.model_admin_url('patch', self.org_obj.pk)}) return HttpResponse(t.render(c))
Example #11
Source File: editable.py From devops with MIT License | 6 votes |
def post(self, request, object_id): model_fields = [f.name for f in self.opts.fields] fields = [f for f in request.POST.keys() if f in model_fields] defaults = { "form": self.form, "fields": fields, "formfield_callback": self.formfield_for_dbfield, } form_class = modelform_factory(self.model, **defaults) form = form_class( instance=self.org_obj, data=request.POST, files=request.FILES) result = {} if form.is_valid(): form.save(commit=True) result['result'] = 'success' result['new_data'] = form.cleaned_data result['new_html'] = dict( [(f, self.get_new_field_html(f)) for f in fields]) else: result['result'] = 'error' result['errors'] = JsonErrorDict(form.errors, form).as_json() return self.render_response(result)
Example #12
Source File: editable.py From imoocc with GNU General Public License v2.0 | 6 votes |
def post(self, request, object_id): model_fields = [f.name for f in self.opts.fields] fields = [f for f in request.POST.keys() if f in model_fields] defaults = { "form": self.form, "fields": fields, "formfield_callback": self.formfield_for_dbfield, } form_class = modelform_factory(self.model, **defaults) form = form_class( instance=self.org_obj, data=request.POST, files=request.FILES) result = {} if form.is_valid(): form.save(commit=True) result['result'] = 'success' result['new_data'] = form.cleaned_data result['new_html'] = dict( [(f, self.get_new_field_html(f)) for f in fields]) else: result['result'] = 'error' result['errors'] = JsonErrorDict(form.errors, form).as_json() return self.render_response(result)
Example #13
Source File: detail.py From myblog with GNU Affero General Public License v3.0 | 6 votes |
def get_model_form(self, **kwargs): """ Returns a Form class for use in the admin add view. This is used by add_view and change_view. """ if self.exclude is None: exclude = [] else: exclude = list(self.exclude) if self.exclude is None and hasattr(self.form, '_meta') and self.form._meta.exclude: # Take the custom ModelForm's Meta.exclude into account only if the # ModelAdmin doesn't define its own. exclude.extend(self.form._meta.exclude) # if exclude is an empty list we pass None to be consistant with the # default on modelform_factory exclude = exclude or None defaults = { "form": self.form, "fields": self.fields and list(self.fields) or '__all__', "exclude": exclude, } defaults.update(kwargs) return modelform_factory(self.model, **defaults)
Example #14
Source File: editable.py From myblog with GNU Affero General Public License v3.0 | 6 votes |
def get(self, request, object_id): model_fields = [f.name for f in self.opts.fields] fields = [f for f in request.GET['fields'].split(',') if f in model_fields] defaults = { "form": self.form, "fields": fields, "formfield_callback": self.formfield_for_dbfield, } form_class = modelform_factory(self.model, **defaults) form = form_class(instance=self.org_obj) helper = FormHelper() helper.form_tag = False helper.include_media = False form.helper = helper s = '{% load i18n crispy_forms_tags %}<form method="post" action="{{action_url}}">{% crispy form %}' + \ '<button type="submit" class="btn btn-success btn-block btn-sm">{% trans "Apply" %}</button></form>' t = template.Template(s) c = template.Context({'form': form, 'action_url': self.model_admin_url('patch', self.org_obj.pk)}) return HttpResponse(t.render(c))
Example #15
Source File: editable.py From myblog with GNU Affero General Public License v3.0 | 6 votes |
def post(self, request, object_id): model_fields = [f.name for f in self.opts.fields] fields = [f for f in request.POST.keys() if f in model_fields] defaults = { "form": self.form, "fields": fields, "formfield_callback": self.formfield_for_dbfield, } form_class = modelform_factory(self.model, **defaults) form = form_class( instance=self.org_obj, data=request.POST, files=request.FILES) result = {} if form.is_valid(): form.save(commit=True) result['result'] = 'success' result['new_data'] = form.cleaned_data result['new_html'] = dict( [(f, self.get_new_field_html(f)) for f in fields]) else: result['result'] = 'error' result['errors'] = JsonErrorDict(form.errors, form).as_json() return self.render_response(result)
Example #16
Source File: detail.py From CTF_AWD_Platform with MIT License | 6 votes |
def get_model_form(self, **kwargs): """ Returns a Form class for use in the admin add view. This is used by add_view and change_view. """ if self.exclude is None: exclude = [] else: exclude = list(self.exclude) if self.exclude is None and hasattr(self.form, '_meta') and self.form._meta.exclude: # Take the custom ModelForm's Meta.exclude into account only if the # ModelAdmin doesn't define its own. exclude.extend(self.form._meta.exclude) # if exclude is an empty list we pass None to be consistant with the # default on modelform_factory exclude = exclude or None defaults = { "form": self.form, "fields": self.fields and list(self.fields) or '__all__', "exclude": exclude, } defaults.update(kwargs) return modelform_factory(self.model, **defaults)
Example #17
Source File: editable.py From CTF_AWD_Platform with MIT License | 6 votes |
def get(self, request, object_id): model_fields = [f.name for f in self.opts.fields] fields = [f for f in request.GET['fields'].split(',') if f in model_fields] defaults = { "form": self.form, "fields": fields, "formfield_callback": self.formfield_for_dbfield, } form_class = modelform_factory(self.model, **defaults) form = form_class(instance=self.org_obj) helper = FormHelper() helper.form_tag = False helper.include_media = False form.helper = helper s = '{% load i18n crispy_forms_tags %}<form method="post" action="{{action_url}}">{% crispy form %}' + \ '<button type="submit" class="btn btn-success btn-block btn-sm">{% trans "Apply" %}</button></form>' t = template.Template(s) c = template.Context({'form': form, 'action_url': self.model_admin_url('patch', self.org_obj.pk)}) return HttpResponse(t.render(c))
Example #18
Source File: editable.py From CTF_AWD_Platform with MIT License | 6 votes |
def post(self, request, object_id): model_fields = [f.name for f in self.opts.fields] fields = [f for f in request.POST.keys() if f in model_fields] defaults = { "form": self.form, "fields": fields, "formfield_callback": self.formfield_for_dbfield, } form_class = modelform_factory(self.model, **defaults) form = form_class( instance=self.org_obj, data=request.POST, files=request.FILES) result = {} if form.is_valid(): form.save(commit=True) result['result'] = 'success' result['new_data'] = form.cleaned_data result['new_html'] = dict( [(f, self.get_new_field_html(f)) for f in fields]) else: result['result'] = 'error' result['errors'] = JsonErrorDict(form.errors, form).as_json() return self.render_response(result)
Example #19
Source File: authentication.py From django-djangui with GNU General Public License v3.0 | 6 votes |
def djangui_login(request): if djangui_settings.DJANGUI_AUTH is False: return HttpResponseRedirect(djangui_settings.DJANGUI_LOGIN_URL) User = get_user_model() form = modelform_factory(User, fields=('username', 'password')) user = User.objects.filter(username=request.POST.get('username')) if user: user = user[0] else: user = None form = form(request.POST, instance=user) if form.is_valid(): data = form.cleaned_data user = authenticate(username=data['username'], password=data['password']) if user is None: return JsonResponse({'valid': False, 'errors': {'__all__': [force_text(_('You have entered an invalid username or password.'))]}}) login(request, user) return JsonResponse({'valid': True, 'redirect': request.POST['next']}) else: return JsonResponse({'valid': False, 'errors': form.errors})
Example #20
Source File: detail.py From django_OA with GNU General Public License v3.0 | 6 votes |
def get_model_form(self, **kwargs): """ Returns a Form class for use in the admin add view. This is used by add_view and change_view. """ if self.exclude is None: exclude = [] else: exclude = list(self.exclude) if self.exclude is None and hasattr(self.form, '_meta') and self.form._meta.exclude: # Take the custom ModelForm's Meta.exclude into account only if the # ModelAdmin doesn't define its own. exclude.extend(self.form._meta.exclude) # if exclude is an empty list we pass None to be consistant with the # default on modelform_factory exclude = exclude or None defaults = { "form": self.form, "fields": self.fields and list(self.fields) or '__all__', "exclude": exclude, } defaults.update(kwargs) return modelform_factory(self.model, **defaults)
Example #21
Source File: editable.py From Mxonline3 with Apache License 2.0 | 6 votes |
def post(self, request, object_id): model_fields = [f.name for f in self.opts.fields] fields = [f for f in request.POST.keys() if f in model_fields] defaults = { "form": self.form, "fields": fields, "formfield_callback": self.formfield_for_dbfield, } form_class = modelform_factory(self.model, **defaults) form = form_class( instance=self.org_obj, data=request.POST, files=request.FILES) result = {} if form.is_valid(): form.save(commit=True) result['result'] = 'success' result['new_data'] = form.cleaned_data result['new_html'] = dict( [(f, self.get_new_field_html(f)) for f in fields]) else: result['result'] = 'error' result['errors'] = JsonErrorDict(form.errors, form).as_json() return self.render_response(result)
Example #22
Source File: editable.py From django_OA with GNU General Public License v3.0 | 6 votes |
def get(self, request, object_id): model_fields = [f.name for f in self.opts.fields] fields = [f for f in request.GET['fields'].split(',') if f in model_fields] defaults = { "form": self.form, "fields": fields, "formfield_callback": self.formfield_for_dbfield, } form_class = modelform_factory(self.model, **defaults) form = form_class(instance=self.org_obj) helper = FormHelper() helper.form_tag = False helper.include_media = False form.helper = helper s = '{% load i18n crispy_forms_tags %}<form method="post" action="{{action_url}}">{% crispy form %}' + \ '<button type="submit" class="btn btn-success btn-block btn-sm">{% trans "Apply" %}</button></form>' t = template.Template(s) c = template.Context({'form': form, 'action_url': self.model_admin_url('patch', self.org_obj.pk)}) return HttpResponse(t.render(c))
Example #23
Source File: editable.py From django_OA with GNU General Public License v3.0 | 6 votes |
def post(self, request, object_id): model_fields = [f.name for f in self.opts.fields] fields = [f for f in request.POST.keys() if f in model_fields] defaults = { "form": self.form, "fields": fields, "formfield_callback": self.formfield_for_dbfield, } form_class = modelform_factory(self.model, **defaults) form = form_class( instance=self.org_obj, data=request.POST, files=request.FILES) result = {} if form.is_valid(): form.save(commit=True) result['result'] = 'success' result['new_data'] = form.cleaned_data result['new_html'] = dict( [(f, self.get_new_field_html(f)) for f in fields]) else: result['result'] = 'error' result['errors'] = JsonErrorDict(form.errors, form).as_json() return self.render_response(result)
Example #24
Source File: editable.py From Mxonline3 with Apache License 2.0 | 6 votes |
def get(self, request, object_id): model_fields = [f.name for f in self.opts.fields] fields = [f for f in request.GET['fields'].split(',') if f in model_fields] defaults = { "form": self.form, "fields": fields, "formfield_callback": self.formfield_for_dbfield, } form_class = modelform_factory(self.model, **defaults) form = form_class(instance=self.org_obj) helper = FormHelper() helper.form_tag = False helper.include_media = False form.helper = helper s = '{% load i18n crispy_forms_tags %}<form method="post" action="{{action_url}}">{% crispy form %}' + \ '<button type="submit" class="btn btn-success btn-block btn-sm">{% trans "Apply" %}</button></form>' t = template.Template(s) c = template.Context({'form': form, 'action_url': self.model_admin_url('patch', self.org_obj.pk)}) return HttpResponse(t.render(c))
Example #25
Source File: detail.py From Mxonline3 with Apache License 2.0 | 6 votes |
def get_model_form(self, **kwargs): """ Returns a Form class for use in the admin add view. This is used by add_view and change_view. """ if self.exclude is None: exclude = [] else: exclude = list(self.exclude) if self.exclude is None and hasattr(self.form, '_meta') and self.form._meta.exclude: # Take the custom ModelForm's Meta.exclude into account only if the # ModelAdmin doesn't define its own. exclude.extend(self.form._meta.exclude) # if exclude is an empty list we pass None to be consistant with the # default on modelform_factory exclude = exclude or None defaults = { "form": self.form, "fields": self.fields and list(self.fields) or '__all__', "exclude": exclude, } defaults.update(kwargs) return modelform_factory(self.model, **defaults)
Example #26
Source File: model.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
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=fields, exclude=exclude )
Example #27
Source File: forms.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_image_form(model): fields = model.admin_form_fields if 'collection' not in fields: # force addition of the 'collection' field, because leaving it out can # cause dubious results when multiple collections exist (e.g adding the # document to the root collection where the user may not have permission) - # and when only one collection exists, it will get hidden anyway. fields = list(fields) + ['collection'] return modelform_factory( model, form=BaseImageForm, fields=fields, formfield_callback=formfield_for_dbfield, # set the 'file' widget to a FileInput rather than the default ClearableFileInput # so that when editing, we don't get the 'currently: ...' banner which is # a bit pointless here widgets={ 'tags': widgets.AdminTagWidget, 'file': forms.FileInput(), 'focal_point_x': forms.HiddenInput(attrs={'class': 'focal_point_x'}), 'focal_point_y': forms.HiddenInput(attrs={'class': 'focal_point_y'}), 'focal_point_width': forms.HiddenInput(attrs={'class': 'focal_point_width'}), 'focal_point_height': forms.HiddenInput(attrs={'class': 'focal_point_height'}), })
Example #28
Source File: forms.py From wagtail with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_document_form(model): fields = model.admin_form_fields if 'collection' not in fields: # force addition of the 'collection' field, because leaving it out can # cause dubious results when multiple collections exist (e.g adding the # document to the root collection where the user may not have permission) - # and when only one collection exists, it will get hidden anyway. fields = list(fields) + ['collection'] return modelform_factory( model, form=BaseDocumentForm, fields=fields, widgets={ 'tags': widgets.AdminTagWidget, 'file': forms.FileInput() })
Example #29
Source File: edit.py From luscan-devel with GNU General Public License v2.0 | 6 votes |
def get_form_class(self): """ Returns the form class to use in this view. """ if self.form_class: return self.form_class else: if self.model is not None: # If a model has been explicitly provided, use it model = self.model elif hasattr(self, 'object') and self.object is not None: # If this view is operating on a single object, use # the class of that object model = self.object.__class__ else: # Try to get a queryset and extract the model class # from that model = self.get_queryset().model return model_forms.modelform_factory(model)
Example #30
Source File: forms.py From django-leonardo with BSD 3-Clause "New" or "Revised" License | 6 votes |
def get_form(self, cls_name, **kwargs): if cls_name not in self._forms: model_cls = get_class(cls_name) form_class_base = getattr( model_cls, 'feincms_item_editor_form', WidgetForm) default_widgets = WIDGETS model_cls.init_widgets() default_widgets.update(getattr(model_cls, 'widgets', {})) self._forms[cls_name] = modelform_factory( model_cls, exclude=[], form=form_class_base, widgets=default_widgets) return self._forms[cls_name]