django.forms.models.inlineformset_factory - python examples

Here are the examples of the python api django.forms.models.inlineformset_factory taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

40 Examples 7

3 View Complete Implementation : formsets.py
Copyright GNU Affero General Public License v3.0
Author : auvipy
    def formset_factory(self):
        """
        Returns the keyword arguments for calling the formset factory
        """
        kwargs = {
            'formset': self.formset_clast,
            'extra': self.extra,
            'max_num': self.max_num,
            'can_order': self.can_order,
            'can_delete': self.can_delete,
            'fields': self.fields,
            'exclude': self.exclude,
            'formfield_callback': self.formfield_callback,
            'fk_name': self.fk_name
        }
        if self.form_clast:
            kwargs['form'] = self.form_clast
        return inlineformset_factory(self.model, self.inline_model, **kwargs)

3 View Complete Implementation : test_product_forms.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : dinoperovic
    def setUp(self):
        self.formset_factory = inlineformset_factory(
            Product,
            AttributeValue,
            formset=AttributeValueInlineFormSet,
            exclude=[],
        )

3 View Complete Implementation : views.py
Copyright BSD 3-Clause "New" or "Revised" License
Author : harikvpy
    def get_formset_clast(self):
        """
        Returns the inline formset clast for adding Books to this author.
        """
        return forms.models.inlineformset_factory(
            Author,
            Book,
            form=CustomBookForm,
            fields=('satle', 'isbn'),
            can_delete=True,
            extra=2)

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    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.astertTrue(formset.is_valid())
        self.astertEqual(Poem.objects.count(), 0)

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_inline_formset_factory(self):
        """
        These should both work without a problem.
        """
        inlineformset_factory(Parent, Child, fk_name='mother', fields="__all__")
        inlineformset_factory(Parent, Child, fk_name='father', fields="__all__")

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_exception_on_unspecified_foreign_key(self):
        """
        Child has two ForeignKeys to Parent, so if we don't specify which one
        to use for the inline formset, we should get an exception.
        """
        msg = "'inline_formsets.Child' has more than one ForeignKey to 'inline_formsets.Parent'."
        with self.astertRaisesMessage(ValueError, msg):
            inlineformset_factory(Parent, Child)

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_fk_name_not_foreign_key_field_from_child(self):
        """
        If we specify fk_name, but it isn't a ForeignKey from the child model
        to the parent model, we should get an exception.
        """
        msg = "fk_name 'school' is not a ForeignKey to 'inline_formsets.Parent'."
        with self.astertRaisesMessage(ValueError, msg):
            inlineformset_factory(Parent, Child, fk_name='school')

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_non_foreign_key_field(self):
        """
        If the field specified in fk_name is not a ForeignKey, we should get an
        exception.
        """
        with self.astertRaisesMessage(ValueError, "'inline_formsets.Child' has no field named 'test'."):
            inlineformset_factory(Parent, Child, fk_name='test')

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    def test_any_iterable_allowed_as_argument_to_exclude(self):
        # Regression test for #9171.
        inlineformset_factory(
            Parent, Child, exclude=['school'], fk_name='mother'
        )

        inlineformset_factory(
            Parent, Child, exclude=('school',), fk_name='mother'
        )

3 View Complete Implementation : tests.py
Copyright GNU Affero General Public License v3.0
Author : nesdis
    @skipUnlessDBFeature('allows_auto_pk_0')
    def test_zero_primary_key(self):
        # Regression test for #21472
        poet = Poet.objects.create(id=0, name='test')
        poet.poem_set.create(name='test poem')
        PoemFormSet = inlineformset_factory(Poet, Poem, fields="__all__", extra=0)
        formset = PoemFormSet(None, instance=poet)
        self.astertEqual(len(formset.forms), 1)