本文整理汇总了Python中django.forms.models.ModelForm方法的典型用法代码示例。如果您正苦于以下问题:Python models.ModelForm方法的具体用法?Python models.ModelForm怎么用?Python models.ModelForm使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.forms.models
的用法示例。
在下文中一共展示了models.ModelForm方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_fieldsets
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import ModelForm [as 别名]
def test_get_fieldsets(self):
# get_fieldsets is called when figuring out form fields.
# Refs #18681.
class MediaForm(ModelForm):
class Meta:
model = Media
fields = '__all__'
class MediaInline(GenericTabularInline):
form = MediaForm
model = Media
can_delete = False
def get_fieldsets(self, request, obj=None):
return [(None, {'fields': ['url', 'description']})]
ma = MediaInline(Media, self.site)
form = ma.get_formset(None).form
self.assertEqual(form._meta.fields, ['url', 'description'])
示例2: test_get_formsets_with_inlines_returns_tuples
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import ModelForm [as 别名]
def test_get_formsets_with_inlines_returns_tuples(self):
"""
get_formsets_with_inlines() returns the correct tuples.
"""
class MediaForm(ModelForm):
class Meta:
model = Media
exclude = ['url']
class MediaInline(GenericTabularInline):
form = MediaForm
model = Media
class AlternateInline(GenericTabularInline):
form = MediaForm
model = Media
class EpisodeAdmin(admin.ModelAdmin):
inlines = [
AlternateInline, MediaInline
]
ma = EpisodeAdmin(Episode, self.site)
inlines = ma.get_inline_instances(request)
for (formset, inline), other_inline in zip(ma.get_formsets_with_inlines(request), inlines):
self.assertIsInstance(formset, other_inline.get_formset(request).__class__)
示例3: get_form
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import ModelForm [as 别名]
def get_form(self, request, obj=None, **kwargs):
form = super(_BaseAdmin, self).get_form(request, obj, **kwargs)
def clean(form):
super(ModelForm, form).clean()
self.clean_form(request, form)
form.clean = clean
return form
示例4: is_model_form
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import ModelForm [as 别名]
def is_model_form(cls):
return issubclass(cls.form_class, ModelForm)
示例5: test_fk_in_all_formset_forms
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import ModelForm [as 别名]
def test_fk_in_all_formset_forms(self):
"""
A foreign key field is in Meta for all forms in the formset (#26538).
"""
class PoemModelForm(ModelForm):
def __init__(self, *args, **kwargs):
assert 'poet' in self._meta.fields
super().__init__(*args, **kwargs)
poet = Poet.objects.create(name='test')
poet.poem_set.create(name='first test poem')
poet.poem_set.create(name='second test poem')
PoemFormSet = inlineformset_factory(Poet, Poem, form=PoemModelForm, fields=('name',), extra=0)
formset = PoemFormSet(None, instance=poet)
formset.forms # Trigger form instantiation to run the assert above.
示例6: test_custom_form_meta_exclude_with_readonly
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import ModelForm [as 别名]
def test_custom_form_meta_exclude_with_readonly(self):
"""
The custom ModelForm's `Meta.exclude` is respected when
used in conjunction with `GenericInlineModelAdmin.readonly_fields`
and when no `ModelAdmin.exclude` is defined.
"""
class MediaForm(ModelForm):
class Meta:
model = Media
exclude = ['url']
class MediaInline(GenericTabularInline):
readonly_fields = ['description']
form = MediaForm
model = Media
class EpisodeAdmin(admin.ModelAdmin):
inlines = [
MediaInline
]
ma = EpisodeAdmin(Episode, self.site)
self.assertEqual(
list(list(ma.get_formsets_with_inlines(request))[0][0]().forms[0].fields),
['keywords', 'id', 'DELETE'])
示例7: smartmodelformset_factory
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import ModelForm [as 别名]
def smartmodelformset_factory(model, request, form=ModelForm, formfield_callback=None,
formset=BaseModelFormSet, extra=1, can_delete=False,
can_order=False, min_num=None, max_num=None, fields=None, exclude=None,
widgets=None, validate_min=False, validate_max=False, localized_fields=None,
labels=None, help_texts=None, error_messages=None,
formreadonlyfield_callback=None, readonly_fields=None,
readonly=False):
meta = getattr(form, 'Meta', None)
if meta is None:
meta = type(str('Meta'), (object,), {})
if getattr(meta, 'fields', fields) is None and getattr(meta, 'exclude', exclude) is None:
warnings.warn("Calling modelformset_factory without defining 'fields' or "
"'exclude' explicitly is deprecated",
PendingDeprecationWarning, stacklevel=2)
form = smartmodelform_factory(
model, request, form=form, fields=fields, exclude=exclude, formfield_callback=formfield_callback,
widgets=widgets, localized_fields=localized_fields, labels=labels, help_texts=help_texts,
error_messages=error_messages, formreadonlyfield_callback=formreadonlyfield_callback,
readonly_fields=readonly_fields, readonly=readonly
)
FormSet = smartformset_factory(
form, formset, extra=extra, min_num=min_num, max_num=max_num, can_order=can_order, can_delete=can_delete,
validate_min=validate_min, validate_max=validate_max
)
FormSet.model = model
return FormSet
示例8: smartinlineformset_factory
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import ModelForm [as 别名]
def smartinlineformset_factory(parent_model, model, request, form=ModelForm,
formset=BaseInlineFormSet, fk_name=None,
fields=None, exclude=None, extra=3, can_order=False,
can_delete=True, min_num=None, max_num=None, formfield_callback=None,
widgets=None, validate_min=False, validate_max=False, localized_fields=None,
labels=None, help_texts=None, error_messages=None,
formreadonlyfield_callback=None, readonly_fields=None,
readonly=False):
fk = _get_foreign_key(parent_model, model, fk_name=fk_name)
# enforce a max_num=1 when the foreign key to the parent model is unique.
if fk.unique:
max_num = 1
kwargs = {
'form': form,
'formfield_callback': formfield_callback,
'formset': formset,
'extra': extra,
'can_delete': can_delete,
'can_order': can_order,
'fields': fields,
'exclude': exclude,
'max_num': max_num,
'min_num': min_num,
'widgets': widgets,
'validate_min': validate_min,
'validate_max': validate_max,
'localized_fields': localized_fields,
'labels': labels,
'help_texts': help_texts,
'error_messages': error_messages,
'formreadonlyfield_callback': formreadonlyfield_callback,
'readonly_fields': readonly_fields,
'readonly': readonly,
}
FormSet = smartmodelformset_factory(model, request, **kwargs)
FormSet.fk = fk
return FormSet
示例9: test_custom_form_meta_exclude
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import ModelForm [as 别名]
def test_custom_form_meta_exclude(self):
"""
The custom ModelForm's `Meta.exclude` is respected by
`GenericInlineModelAdmin.get_formset`, and overridden if
`ModelAdmin.exclude` or `GenericInlineModelAdmin.exclude` are defined.
Refs #15907.
"""
# First with `GenericInlineModelAdmin` -----------------
class MediaForm(ModelForm):
class Meta:
model = Media
exclude = ['url']
class MediaInline(GenericTabularInline):
exclude = ['description']
form = MediaForm
model = Media
class EpisodeAdmin(admin.ModelAdmin):
inlines = [
MediaInline
]
ma = EpisodeAdmin(Episode, self.site)
self.assertEqual(
list(list(ma.get_formsets_with_inlines(request))[0][0]().forms[0].fields),
['url', 'keywords', 'id', 'DELETE'])
# Then, only with `ModelForm` -----------------
class MediaInline(GenericTabularInline):
form = MediaForm
model = Media
class EpisodeAdmin(admin.ModelAdmin):
inlines = [
MediaInline
]
ma = EpisodeAdmin(Episode, self.site)
self.assertEqual(
list(list(ma.get_formsets_with_inlines(request))[0][0]().forms[0].fields),
['description', 'keywords', 'id', 'DELETE'])
示例10: smart_generic_inlineformset_factory
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import ModelForm [as 别名]
def smart_generic_inlineformset_factory(model, request, form=ModelForm, formset=BaseGenericInlineFormSet,
ct_field='content_type', fk_field='object_id', fields=None, exclude=None,
extra=3, can_order=False, can_delete=True, min_num=None, max_num=None,
formfield_callback=None, widgets=None, validate_min=False, validate_max=False,
localized_fields=None, labels=None, help_texts=None, error_messages=None,
formreadonlyfield_callback=None, readonly_fields=None, for_concrete_model=True,
readonly=False):
"""
Returns a ``GenericInlineFormSet`` for the given kwargs.
You must provide ``ct_field`` and ``fk_field`` if they are different from
the defaults ``content_type`` and ``object_id`` respectively.
"""
opts = model._meta
# if there is no field called `ct_field` let the exception propagate
ct_field = opts.get_field(ct_field)
if not isinstance(ct_field, models.ForeignKey) or ct_field.related_model != ContentType:
raise Exception("fk_name '%s' is not a ForeignKey to ContentType" % ct_field)
fk_field = opts.get_field(fk_field) # let the exception propagate
if exclude is not None:
exclude = list(exclude)
exclude.extend([ct_field.name, fk_field.name])
else:
exclude = [ct_field.name, fk_field.name]
kwargs = {
'form': form,
'formfield_callback': formfield_callback,
'formset': formset,
'extra': extra,
'can_delete': can_delete,
'can_order': can_order,
'fields': fields,
'exclude': exclude,
'max_num': max_num,
'min_num': min_num,
'widgets': widgets,
'validate_min': validate_min,
'validate_max': validate_max,
'localized_fields': localized_fields,
'formreadonlyfield_callback': formreadonlyfield_callback,
'readonly_fields': readonly_fields,
'readonly': readonly,
'labels': labels,
'help_texts': help_texts,
'error_messages': error_messages,
}
FormSet = smartmodelformset_factory(model, request, **kwargs)
FormSet.ct_field = ct_field
FormSet.ct_fk_field = fk_field
FormSet.for_concrete_model = for_concrete_model
return FormSet