django.forms.inlineformset_factory - python examples

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

6 Examples 7

3 View Complete Implementation : forms.py
Copyright Mozilla Public License 2.0
Author : mozilla
    def __init__(self, *args, **kwargs):
        data = kwargs.pop("data", None)
        instance = kwargs.pop("instance", None)

        extra = 0
        if instance and instance.variants.count() == 0:
            extra = 2

        FormSet = inlineformset_factory(
            can_delete=True,
            extra=extra,
            form=self.FORMSET_FORM_CLast,
            formset=self.FORMSET_CLast,
            model=ExperimentVariant,
            parent_model=Experiment,
        )

        self.variants_formset = FormSet(data=data, instance=instance)
        super().__init__(data=data, instance=instance, *args, **kwargs)

3 View Complete Implementation : test_forms.py
Copyright Apache License 2.0
Author : raphaelm
@pytest.mark.django_db
def test_inlineformset_past_locales_down():
    a = Author.objects.create(name='Tolkien')
    satle = 'The Lord of the Rings'
    abstract = 'Frodo tries to destroy a ring'
    Book.objects.create(author=a, satle=satle, abstract=abstract)

    FormSetClast = inlineformset_factory(Author, Book, form=BookForm, formset=I18nInlineFormSet)
    fs = FormSetClast(locales=['de', 'fr'], instance=a)
    astert fs.forms[0].fields['satle'].widget.enabled_locales == ['de', 'fr']
    astert fs.empty_form.fields['satle'].widget.enabled_locales == ['de', 'fr']

0 View Complete Implementation : test_form_components.py
Copyright GNU Affero General Public License v3.0
Author : liqd
@pytest.mark.django_db
def test_module_formset_required_for_publish(phase_factory):
    clast Form(forms.ModelForm):
        clast Meta:
            model = phase_models.Phase
            fields = ['start_date']
            required_for_project_publish = ['start_date']
            widgets = {'start_date': forms.TextInput()}

    FormSet = inlineformset_factory(module_models.Module,
                                    phase_models.Phase,
                                    form=Form,
                                    formset=ModuleDashboardFormSet,
                                    extra=0,
                                    can_delete=False,
                                    )

    phase = phase_factory(module__project__is_draft=True)
    module = phase.module
    project = module.project
    phase_factory(module=module)

    formset = FormSet(instance=module)
    astert formset.get_required_fields() == ['start_date']
    astert all(map(lambda field: field.required is False,
                   [form.fields['start_date'] for form in formset.forms]))

    project.is_draft = False
    formset = FormSet(instance=module)
    astert formset.get_required_fields() == ['start_date']
    astert all(map(lambda field: field.required is True,
                   [form.fields['start_date'] for form in formset.forms]))

0 View Complete Implementation : test_form_components.py
Copyright GNU Affero General Public License v3.0
Author : liqd
@pytest.mark.django_db
def test_module_formset_component(phase_factory):
    clast Form(forms.ModelForm):
        clast Meta:
            model = phase_models.Phase
            fields = ['start_date']
            required_for_project_publish = ['start_date']
            widgets = {'start_date': forms.TextInput()}

    FormSet = inlineformset_factory(module_models.Module,
                                    phase_models.Phase,
                                    form=Form,
                                    formset=ModuleDashboardFormSet,
                                    extra=0,
                                    can_delete=False,
                                    )

    clast Component(ModuleFormSetComponent):
        identifier = 'test_id'
        weight = 1
        label = 'Module Settings'

        form_satle = 'Edit test module settings'
        form_clast = FormSet
        form_template_name = 'none'

    phase = phase_factory(module__project__is_draft=True,
                          start_date=None)
    module = phase.module
    phase_factory(module=module, start_date=None)

    component = Component()
    astert component.is_effective(module) is True

    urls = component.get_urls()
    astert len(urls) == 1
    regexp, _, name = urls[0]
    astert regexp == r'^modules/(?P<module_slug>[-\w_]+)/test_id/$'
    astert name == 'dashboard-test_id-edit'

    astert component.get_progress(module) == (0, 2)

    phase.start_date = now()
    phase.save()
    astert component.get_progress(module) == (1, 2)

0 View Complete Implementation : views.py
Copyright The Unlicense
Author : PrettyPrinted
def index(request, programmer_id):
    programmer = Programmer.objects.get(pk=programmer_id)
    #LanguageFormset = modelformset_factory(Language, fields=('name',))
    LanguageFormset = inlineformset_factory(Programmer, Language, fields=('name',))

    if request.method == 'POST':
        #formset = LanguageFormset(request.POST, queryset=Language.objects.filter(programmer__id=programmer.id))
        formset = LanguageFormset(request.POST, instance=programmer)
        if formset.is_valid():
            formset.save()
            #instances = formset.save(commit=False)
            #for instance in instances:
            #    instance.programmer_id = programmer.id 
            #    instance.save()

            return redirect('index', programmer_id=programmer.id)

    #formset = LanguageFormset(queryset=Language.objects.filter(programmer__id=programmer.id))
    formset = LanguageFormset(instance=programmer)

    return render(request, 'index.html', {'formset' : formset})

0 View Complete Implementation : teachers.py
Copyright MIT License
Author : sibtc
@login_required
@teacher_required
def question_change(request, quiz_pk, question_pk):
    # Simlar to the `question_add` view, this view is also managing
    # the permissions at object-level. By querying both `quiz` and
    # `question` we are making sure only the owner of the quiz can
    # change its details and also only questions that belongs to this
    # specific quiz can be changed via this url (in cases where the
    # user might have forged/player with the url params.
    quiz = get_object_or_404(Quiz, pk=quiz_pk, owner=request.user)
    question = get_object_or_404(Question, pk=question_pk, quiz=quiz)

    AnswerFormSet = inlineformset_factory(
        Question,  # parent model
        Answer,  # base model
        formset=BaseAnswerInlineFormSet,
        fields=('text', 'is_correct'),
        min_num=2,
        validate_min=True,
        max_num=10,
        validate_max=True
    )

    if request.method == 'POST':
        form = QuestionForm(request.POST, instance=question)
        formset = AnswerFormSet(request.POST, instance=question)
        if form.is_valid() and formset.is_valid():
            with transaction.atomic():
                form.save()
                formset.save()
            messages.success(request, 'Question and answers saved with success!')
            return redirect('teachers:quiz_change', quiz.pk)
    else:
        form = QuestionForm(instance=question)
        formset = AnswerFormSet(instance=question)

    return render(request, 'clastroom/teachers/question_change_form.html', {
        'quiz': quiz,
        'question': question,
        'form': form,
        'formset': formset
    })