本文整理汇总了Python中django.forms.models.modelform_factory方法的典型用法代码示例。如果您正苦于以下问题:Python models.modelform_factory方法的具体用法?Python models.modelform_factory怎么用?Python models.modelform_factory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.forms.models
的用法示例。
在下文中一共展示了models.modelform_factory方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_model_form
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import modelform_factory [as 别名]
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)
示例2: get
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import modelform_factory [as 别名]
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))
示例3: post
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import modelform_factory [as 别名]
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)
示例4: djangui_login
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import modelform_factory [as 别名]
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})
示例5: get_form_class
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import modelform_factory [as 别名]
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
)
示例6: get_image_form
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import modelform_factory [as 别名]
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'}),
})
示例7: get_document_form
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import modelform_factory [as 别名]
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()
})
示例8: get_form
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import modelform_factory [as 别名]
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]
示例9: get_form_class
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import modelform_factory [as 别名]
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)
示例10: get_model_form
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import modelform_factory [as 别名]
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)
示例11: get
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import modelform_factory [as 别名]
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))