本文整理汇总了Python中django.forms.models.inlineformset_factory方法的典型用法代码示例。如果您正苦于以下问题:Python models.inlineformset_factory方法的具体用法?Python models.inlineformset_factory怎么用?Python models.inlineformset_factory使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.forms.models
的用法示例。
在下文中一共展示了models.inlineformset_factory方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import inlineformset_factory [as 别名]
def get(self, request, *args, **kwargs):
"""Overriding get method to handle inline formset."""
# Setup the formset for PlanCost
PlanCostFormSet = inlineformset_factory( # pylint: disable=invalid-name
parent_model=models.SubscriptionPlan,
model=models.PlanCost,
form=forms.PlanCostForm,
can_delete=False,
extra=1,
)
self.object = None
form = self.get_form(self.get_form_class())
cost_forms = PlanCostFormSet()
return self.render_to_response(
self.get_context_data(
form=form,
cost_forms=cost_forms,
)
)
示例2: post
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import inlineformset_factory [as 别名]
def post(self, request, *args, **kwargs):
"""Overriding post method to handle inline formsets."""
# Setup the formset for PlanCost
PlanCostFormSet = inlineformset_factory( # pylint: disable=invalid-name
parent_model=models.SubscriptionPlan,
model=models.PlanCost,
form=forms.PlanCostForm,
can_delete=False,
extra=1,
)
self.object = None
form = self.get_form(self.get_form_class())
cost_forms = PlanCostFormSet(self.request.POST)
if form.is_valid() and cost_forms.is_valid():
return self.form_valid(form, cost_forms)
return self.form_invalid(form, cost_forms)
示例3: test_deletion
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import inlineformset_factory [as 别名]
def test_deletion(self):
PoemFormSet = inlineformset_factory(Poet, Poem, can_delete=True, fields="__all__")
poet = Poet.objects.create(name='test')
poem = poet.poem_set.create(name='test poem')
data = {
'poem_set-TOTAL_FORMS': '1',
'poem_set-INITIAL_FORMS': '1',
'poem_set-MAX_NUM_FORMS': '0',
'poem_set-0-id': str(poem.pk),
'poem_set-0-poet': str(poet.pk),
'poem_set-0-name': 'test',
'poem_set-0-DELETE': 'on',
}
formset = PoemFormSet(data, instance=poet)
formset.save()
self.assertTrue(formset.is_valid())
self.assertEqual(Poem.objects.count(), 0)
示例4: test_save_new
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import inlineformset_factory [as 别名]
def test_save_new(self):
"""
Make sure inlineformsets respect commit=False
regression for #10750
"""
# exclude some required field from the forms
ChildFormSet = inlineformset_factory(School, Child, exclude=['father', 'mother'])
school = School.objects.create(name='test')
mother = Parent.objects.create(name='mother')
father = Parent.objects.create(name='father')
data = {
'child_set-TOTAL_FORMS': '1',
'child_set-INITIAL_FORMS': '0',
'child_set-MAX_NUM_FORMS': '0',
'child_set-0-name': 'child',
}
formset = ChildFormSet(data, instance=school)
self.assertIs(formset.is_valid(), True)
objects = formset.save(commit=False)
for obj in objects:
obj.mother = mother
obj.father = father
obj.save()
self.assertEqual(school.child_set.count(), 1)
示例5: user_profile_save_locations
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import inlineformset_factory [as 别名]
def user_profile_save_locations(request):
profile = request.user.userprofile
LocationsFormset = inlineformset_factory(
UserProfile, Location, form=LocationEditForm, extra=1)
formset = LocationsFormset(data=request.POST, instance=profile)
if not formset.is_valid():
messages.error(request,
_("There was one or more errors processing the form. You may need to scroll down to see them."))
return render(request, 'user/profile/edit/locations.html', {
'formset': formset,
'profile': profile,
})
formset.save()
messages.success(request, _("Form saved. Thank you!"))
return HttpResponseRedirect('/profile/edit/locations/')
示例6: rule_formset_factory
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import inlineformset_factory [as 别名]
def rule_formset_factory(grade_source, reqpost=None):
if grade_source.scale == 'DISC':
DiscreteRuleFormSet = inlineformset_factory(GradeSource, DiscreteRule, can_delete=True, extra=10)
formset = DiscreteRuleFormSet(reqpost, instance=grade_source)
else:
ContinuousRuleFormSet = inlineformset_factory(GradeSource, ContinuousRule, can_delete=True, extra=10)
formset = ContinuousRuleFormSet(reqpost, instance=grade_source)
return formset
示例7: get_formset
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import inlineformset_factory [as 别名]
def get_formset(self, **kwargs):
"""Returns a BaseInlineFormSet class for use in admin add/change views."""
if self.exclude is None:
exclude = []
else:
exclude = list(self.exclude)
exclude.extend(self.get_readonly_fields())
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
# InlineModelAdmin doesn't define its own.
exclude.extend(self.form._meta.exclude)
# if exclude is an empty list we use None, since that's the actual
# default
exclude = exclude or None
can_delete = self.can_delete and self.has_delete_permission()
defaults = {
"form": self.form,
"formset": self.formset,
"fk_name": self.fk_name,
'fields': forms.ALL_FIELDS,
"exclude": exclude,
"formfield_callback": self.formfield_for_dbfield,
"extra": self.extra,
"max_num": self.max_num,
"can_delete": can_delete,
}
defaults.update(kwargs)
return inlineformset_factory(self.parent_model, self.model, **defaults)
示例8: construct_formset
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import inlineformset_factory [as 别名]
def construct_formset(self):
"""
Returns an instance of the inline formset
"""
if not self.inline_model or not self.parent_model:
msg = "Parent and Inline models are required in {}".format(self.__class__.__name__)
raise NotModelException(msg)
return inlineformset_factory(
self.parent_model,
self.inline_model,
**self.get_factory_kwargs())
示例9: test_inlineformset_factory_nulls_default_pks
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import inlineformset_factory [as 别名]
def test_inlineformset_factory_nulls_default_pks(self):
"""
#24377 - If we're adding a new object, a parent's auto-generated pk
from the model field default should be ignored as it's regenerated on
the save request.
Tests the case where both the parent and child have a UUID primary key.
"""
FormSet = inlineformset_factory(UUIDPKParent, UUIDPKChild, fields='__all__')
formset = FormSet()
self.assertIsNone(formset.forms[0].fields['parent'].initial)
示例10: test_inlineformset_factory_ignores_default_pks_on_submit
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import inlineformset_factory [as 别名]
def test_inlineformset_factory_ignores_default_pks_on_submit(self):
"""
#24377 - Inlines with a model field default should ignore that default
value to avoid triggering validation on empty forms.
"""
FormSet = inlineformset_factory(UUIDPKParent, UUIDPKChild, fields='__all__')
formset = FormSet({
'uuidpkchild_set-TOTAL_FORMS': 3,
'uuidpkchild_set-INITIAL_FORMS': 0,
'uuidpkchild_set-MAX_NUM_FORMS': '',
'uuidpkchild_set-0-name': 'Foo',
'uuidpkchild_set-1-name': '',
'uuidpkchild_set-2-name': '',
})
self.assertTrue(formset.is_valid())
示例11: test_inlineformset_factory_nulls_default_pks_uuid_parent_auto_child
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import inlineformset_factory [as 别名]
def test_inlineformset_factory_nulls_default_pks_uuid_parent_auto_child(self):
"""
#24958 - Variant of test_inlineformset_factory_nulls_default_pks for
the case of a parent object with a UUID primary key and a child object
with an AutoField primary key.
"""
FormSet = inlineformset_factory(UUIDPKParent, AutoPKChildOfUUIDPKParent, fields='__all__')
formset = FormSet()
self.assertIsNone(formset.forms[0].fields['parent'].initial)
示例12: test_inlineformset_factory_nulls_default_pks_auto_parent_uuid_child
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import inlineformset_factory [as 别名]
def test_inlineformset_factory_nulls_default_pks_auto_parent_uuid_child(self):
"""
#24958 - Variant of test_inlineformset_factory_nulls_default_pks for
the case of a parent object with an AutoField primary key and a child
object with a UUID primary key.
"""
FormSet = inlineformset_factory(AutoPKParent, UUIDPKChildOfAutoPKParent, fields='__all__')
formset = FormSet()
self.assertIsNone(formset.forms[0].fields['parent'].initial)
示例13: test_inlineformset_factory_nulls_default_pks_child_editable_pk
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import inlineformset_factory [as 别名]
def test_inlineformset_factory_nulls_default_pks_child_editable_pk(self):
"""
#24958 - Variant of test_inlineformset_factory_nulls_default_pks for
the case of a parent object with a UUID primary key and a child
object with an editable natural key for a primary key.
"""
FormSet = inlineformset_factory(UUIDPKParent, ChildWithEditablePK, fields='__all__')
formset = FormSet()
self.assertIsNone(formset.forms[0].fields['parent'].initial)
示例14: test_add_form_deletion_when_invalid
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import inlineformset_factory [as 别名]
def test_add_form_deletion_when_invalid(self):
"""
Make sure that an add form that is filled out, but marked for deletion
doesn't cause validation errors.
"""
PoemFormSet = inlineformset_factory(Poet, Poem, can_delete=True, fields="__all__")
poet = Poet.objects.create(name='test')
data = {
'poem_set-TOTAL_FORMS': '1',
'poem_set-INITIAL_FORMS': '0',
'poem_set-MAX_NUM_FORMS': '0',
'poem_set-0-id': '',
'poem_set-0-poem': '1',
'poem_set-0-name': 'x' * 1000,
}
formset = PoemFormSet(data, instance=poet)
# Make sure this form doesn't pass validation.
self.assertIs(formset.is_valid(), False)
self.assertEqual(Poem.objects.count(), 0)
# Then make sure that it *does* pass validation and delete the object,
# even though the data isn't actually valid.
data['poem_set-0-DELETE'] = 'on'
formset = PoemFormSet(data, instance=poet)
self.assertIs(formset.is_valid(), True)
formset.save()
self.assertEqual(Poem.objects.count(), 0)
示例15: test_change_form_deletion_when_invalid
# 需要导入模块: from django.forms import models [as 别名]
# 或者: from django.forms.models import inlineformset_factory [as 别名]
def test_change_form_deletion_when_invalid(self):
"""
Make sure that a change form that is filled out, but marked for deletion
doesn't cause validation errors.
"""
PoemFormSet = inlineformset_factory(Poet, Poem, can_delete=True, fields="__all__")
poet = Poet.objects.create(name='test')
poem = poet.poem_set.create(name='test poem')
data = {
'poem_set-TOTAL_FORMS': '1',
'poem_set-INITIAL_FORMS': '1',
'poem_set-MAX_NUM_FORMS': '0',
'poem_set-0-id': str(poem.id),
'poem_set-0-poem': str(poem.id),
'poem_set-0-name': 'x' * 1000,
}
formset = PoemFormSet(data, instance=poet)
# Make sure this form doesn't pass validation.
self.assertIs(formset.is_valid(), False)
self.assertEqual(Poem.objects.count(), 1)
# Then make sure that it *does* pass validation and delete the object,
# even though the data isn't actually valid.
data['poem_set-0-DELETE'] = 'on'
formset = PoemFormSet(data, instance=poet)
self.assertIs(formset.is_valid(), True)
formset.save()
self.assertEqual(Poem.objects.count(), 0)