我们从Python开源项目中,提取了以下23个代码示例,用于说明如何使用django.forms.modelform_factory()。
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) config = Configuration.get_solo().registration_form or [] config = sorted( [field for field in config if field['position'] is not None], key=lambda field: field['position'] ) profiles = { profile.related_model.__name__: profile.related_model for profile in Member._meta.related_objects if isinstance(profile, OneToOneRel) and profile.name.startswith('profile_') } for field in config: model_name = field['name'].split('__')[0] if model_name in MAPPING: model = MAPPING[model_name] else: model = profiles[field['name'].split('__')[0]] temp_form = forms.modelform_factory(model, fields=[field['name'].split('__')[-1]])() form_field = [field for field in temp_form.fields.values()][0] form_field.model = model self.fields[field['name']] = form_field
def get_forms(self): obj = self.get_object() return [ self._instantiate(forms.modelform_factory(Member, fields=['name', 'number', 'address', 'email']), member=obj, instance=obj), self._instantiate(forms.modelform_factory(Membership, fields=['start', 'interval', 'amount']), member=obj, instance=obj.memberships.first()) ] + [ self._instantiate(forms.modelform_factory( profile_class, fields=[f.name for f in profile_class._meta.fields if f.name not in ['id', 'member']], ), member=obj, profile_class=profile_class) for profile_class in obj.profile_classes ]
def test_string_list_model_form_field(self): form_class = modelform_factory(StringListModel, fields=('values',)) form = form_class() field = form.fields['values'] self.assertIsInstance(field, CollectionField) self.assertEqual(field.collection_type, list) self.assertEqual(field.item_type, six.text_type) self.assertFalse(field.sort) self.assertIsNone(field.max_items) self.assertTrue(field.required) self.assertEqual(field.max_length, 1024)
def test_valid_string_list_model_form_save(self): form_class = modelform_factory(StringListModel, fields=('values',)) form = form_class({'values': "This,is,a,test"}) self.assertTrue(form.is_valid()) self.assertEqual( form.cleaned_data['values'], ['This', 'is', 'a', 'test'] ) obj = form.save() self.assertEqual(obj.values, ['This', 'is', 'a', 'test'])
def test_integer_tuple_model_form_field(self): form_class = modelform_factory(IntegerTupleModel, fields=('values',)) form = form_class() field = form.fields['values'] self.assertIsInstance(field, CollectionField) self.assertEqual(field.collection_type, tuple) self.assertEqual(field.item_type, int) self.assertFalse(field.sort) self.assertIsNone(field.max_items) self.assertTrue(field.required) self.assertEqual(field.max_length, 1024)
def test_valid_integer_tuple_model_form_save(self): form_class = modelform_factory(IntegerTupleModel, fields=('values',)) form = form_class({'values': "12, 22, 9, -5"}) self.assertTrue(form.is_valid()) self.assertEqual( form.cleaned_data['values'], (12, 22, 9, -5) ) obj = form.save() self.assertEqual(obj.values, (12, 22, 9, -5))
def test_decimal_set_model_form_field(self): form_class = modelform_factory(DecimalSetModel, fields=('values',)) form = form_class() field = form.fields['values'] self.assertIsInstance(field, CollectionField) self.assertEqual(field.collection_type, set) self.assertEqual(field.item_type, Decimal) self.assertFalse(field.sort) self.assertIsNone(field.max_items) self.assertTrue(field.required) self.assertEqual(field.max_length, 1024)
def test_required_model_form_field_param(self): form_class = modelform_factory( OptionalIntegerTupleModel, fields=('values',) ) field = form_class().fields['values'] self.assertIsInstance(field, CollectionField) self.assertFalse(field.required)
def test_unique_items_model_form_field_param(self): form_class = modelform_factory( SortedUniqueStringListModel, fields=('values',) ) field = form_class().fields['values'] self.assertIsInstance(field, CollectionField) self.assertTrue(field.unique_items)
def test_sort_model_form_field_param(self): form_class = modelform_factory( SortedDecimalListModel, fields=('values',) ) field = form_class().fields['values'] self.assertIsInstance(field, CollectionField) self.assertTrue(field.sort)
def test_max_items_model_form_field_param(self): form_class = modelform_factory( Max5ItemsStringListModel, fields=('values',) ) field = form_class().fields['values'] self.assertIsInstance(field, CollectionField) self.assertEqual(field.max_items, 5)
def test_max_length_model_form_field_param(self): form_class = modelform_factory( Max10CharsStringListModel, fields=('values',) ) field = form_class().fields['values'] self.assertIsInstance(field, CollectionField) self.assertEqual(field.max_length, 10)
def test_choice_string_list_model_form_field(self): form_class = modelform_factory( ChoiceStringListModel, fields=('values',) ) form = form_class() field = form.fields['values'] self.assertIsInstance(field, CollectionChoiceField) self.assertEqual(field.collection_type, list) self.assertEqual(field.item_type, six.text_type) self.assertFalse(field.sort) self.assertIsNone(field.max_items) self.assertTrue(field.required) self.assertEqual(field.max_length, 1024)
def test_valid_choice_string_list_model_form_save(self): form_class = modelform_factory( ChoiceStringListModel, fields=('values',) ) form = form_class({'values': ['aaa', 'bbb']}) self.assertTrue(form.is_valid()) self.assertEqual( form.cleaned_data['values'], ['aaa', 'bbb'] ) obj = form.save() self.assertEqual(obj.values, ['aaa', 'bbb'])
def test_choice_integer_list_model_form_field(self): form_class = modelform_factory( OptionalGroupedChoiceIntegerListModel, fields=('values',) ) form = form_class() field = form.fields['values'] self.assertIsInstance(field, CollectionChoiceField) self.assertEqual(field.collection_type, list) self.assertEqual(field.item_type, int) self.assertFalse(field.sort) self.assertIsNone(field.max_items) self.assertFalse(field.required) self.assertEqual(field.max_length, 1024)
def test_valid_choice_integer_list_model_form_field(self): form_class = modelform_factory( OptionalGroupedChoiceIntegerListModel, fields=('values',) ) form = form_class({'values': [12, 21]}) self.assertTrue(form.is_valid()) self.assertEqual( form.cleaned_data['values'], [12, 21] ) obj = form.save() self.assertEqual(obj.values, [12, 21])
def test_valid_blank_choice_integer_list_model_form_field(self): form_class = modelform_factory( OptionalGroupedChoiceIntegerListModel, fields=('values',) ) form = form_class({}) self.assertTrue(form.is_valid()) self.assertEqual( form.cleaned_data['values'], [] ) obj = form.save() self.assertEqual(obj.values, [])
def get_form_class(self): return modelform_factory( self.model, form=ParticipantForm, fields=['name', 'vip', 'email', 'phone_number', 'biography', 'notes'] + ParticipantForm.SOCIAL_FIELDS, )
def form_class_from_model(self): if self.model_name in self.model_form_fields: Form = modelform_factory(self.model, fields=self.model_form_fields[self.model_name], widgets={'user': widgets.TextInput}) else: Form = modelform_factory(self.model, exclude=self.excluded_fields, widgets={'user': widgets.TextInput}) return Form
def get_form_class(self): """ Method for generating a form class for the view :return: ModelForm class """ return modelform_factory( self.model, exclude=self.exclude, form=self.form_class, widgets=self._get_form_widgets(), help_texts=self._get_help_texts(), )
def get_form_class(self): """ Method for generating a form class from the data contained within the model :return: ModelForm class """ return modelform_factory( self.content_type.model_class(), form=self._get_base_form_class(), fields=self.used_field_names, formfield_callback=self.formfield_callback )
def get_people_form_class(person_form_instance): form_class = forms.modelform_factory(Person, fields=person_form_instance.personal_information, form=BasePersonForm) form_class.person_form_instance = person_form_instance return form_class