Python django.forms 模块,modelform_factory() 实例源码

我们从Python开源项目中,提取了以下23个代码示例,用于说明如何使用django.forms.modelform_factory()

项目:byro    作者:byro    | 项目源码 | 文件源码
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
项目:byro    作者:byro    | 项目源码 | 文件源码
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
        ]
项目:django-collectionfield    作者:escer    | 项目源码 | 文件源码
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)
项目:django-collectionfield    作者:escer    | 项目源码 | 文件源码
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'])
项目:django-collectionfield    作者:escer    | 项目源码 | 文件源码
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)
项目:django-collectionfield    作者:escer    | 项目源码 | 文件源码
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))
项目:django-collectionfield    作者:escer    | 项目源码 | 文件源码
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)
项目:django-collectionfield    作者:escer    | 项目源码 | 文件源码
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)
项目:django-collectionfield    作者:escer    | 项目源码 | 文件源码
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)
项目:django-collectionfield    作者:escer    | 项目源码 | 文件源码
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)
项目:django-collectionfield    作者:escer    | 项目源码 | 文件源码
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)
项目:django-collectionfield    作者:escer    | 项目源码 | 文件源码
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)
项目:django-collectionfield    作者:escer    | 项目源码 | 文件源码
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)
项目:django-collectionfield    作者:escer    | 项目源码 | 文件源码
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'])
项目:django-collectionfield    作者:escer    | 项目源码 | 文件源码
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)
项目:django-collectionfield    作者:escer    | 项目源码 | 文件源码
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])
项目:django-collectionfield    作者:escer    | 项目源码 | 文件源码
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, [])
项目:PonyConf    作者:PonyConf    | 项目源码 | 文件源码
def get_form_class(self):
        return modelform_factory(
                    self.model,
                    form=ParticipantForm,
                    fields=['name', 'vip', 'email', 'phone_number', 'biography', 'notes'] + ParticipantForm.SOCIAL_FIELDS,
        )
项目:PonyConf    作者:PonyConf    | 项目源码 | 文件源码
def get_form_class(self):
        return modelform_factory(
                    self.model,
                    form=ParticipantForm,
                    fields=['name', 'vip', 'email', 'phone_number', 'biography', 'notes'] + ParticipantForm.SOCIAL_FIELDS,
        )
项目:djragondrop    作者:petroleyum    | 项目源码 | 文件源码
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
项目:omni-forms    作者:omni-digital    | 项目源码 | 文件源码
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(),
        )
项目:omni-forms    作者:omni-digital    | 项目源码 | 文件源码
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
        )
项目:api-django    作者:lafranceinsoumise    | 项目源码 | 文件源码
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