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

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

项目:fir_async_plugin    作者:gcrahay    | 项目源码 | 文件源码
def preferences(request):

    class NotificationPreferenceForm(forms.ModelForm):
        event = forms.ChoiceField(choices=registry.get_event_choices(), disabled=True, widget=forms.HiddenInput())
        method = forms.ChoiceField(choices=registry.get_method_choices(), disabled=True, widget=forms.HiddenInput())
        business_lines = forms.ModelMultipleChoiceField(BusinessLine.authorization.for_user(request.user,
                                                                                            'incidents.view_incidents'),
                                                        required=False)

        class Meta:
            fields = "__all__"

    formset = forms.inlineformset_factory(User, NotificationPreference,
                                          formset=NotificationPreferenceFormset,
                                          form=NotificationPreferenceForm)
    if request.method == 'POST':
        fs = formset(request.POST, instance=request.user)
        if fs.is_valid():
            fs.save()
        return redirect('user:profile')
    else:
        fs = formset(instance=request.user)

    return render(request, "fir_async/preferences.html", {'formset': fs})
项目:economy-game    作者:Aearsis    | 项目源码 | 文件源码
def create_auction(request):
    AIFormSet = inlineformset_factory(Auction, AuctionedItem, form=AuctionedItemForm, can_delete=False, extra=1)
    if request.method == "POST":
        form = CreateAuctionForm(request.POST)
        formset = AIFormSet(request.POST)
        if form.is_valid() and formset.is_valid():
            try:
                data = form.cleaned_data
                data['auctioneditems'] = formset.cleaned_data
                auction = WhiteAuction.create(request.team, data)
                messages.add_message(request, messages.SUCCESS, "Aukce byla vytvo?ena.")
                Status.add("Tým %s práv? vystavil aukci!" % request.team.name, Status.SUCCESS)
                return redirect(reverse("detail", args=(auction.id,)))
            except InvalidTransaction as e:
                form.add_error(None, str(e))
    else:
        form = CreateAuctionForm()
        formset = AIFormSet()

    return render(request, "auctions/create_auction.html", {
        'form': form,
        'items': formset,
        'empty_item': formset.empty_form,
        'balance': request.team.balance_set.all(),
    })
项目:csv_generator    作者:fatboystring    | 项目源码 | 文件源码
def setUp(self):
        super(CsvGeneratorColumnFormSetTestCase, self).setUp()
        self.formset = forms.inlineformset_factory(
            CsvGenerator,
            CsvGeneratorColumn,
            formset=CsvGeneratorColumnFormSet,
            form=CsvGeneratorColumnForm,
            exclude=()
        )
项目:django-i18nfield    作者:raphaelm    | 项目源码 | 文件源码
def test_inlineformset_pass_locales_down():
    a = Author.objects.create(name='Tolkien')
    title = 'The Lord of the Rings'
    abstract = 'Frodo tries to destroy a ring'
    Book.objects.create(author=a, title=title, abstract=abstract)

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