第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

單個(gè)Django ModelForm中有多個(gè)模型?

單個(gè)Django ModelForm中有多個(gè)模型?

胡子哥哥 2019-12-12 13:50:48
ModelFormDjango 是否可以在一個(gè)模型中包含多個(gè)模型?我正在嘗試創(chuàng)建個(gè)人資料編輯表單。因此,我需要包括User模型和 UserProfile模型中的某些字段。目前我正在使用2種形式class UserEditForm(ModelForm):    class Meta:        model = User        fields = ("first_name", "last_name")class UserProfileForm(ModelForm):    class Meta:        model = UserProfile        fields = ("middle_name", "home_phone", "work_phone", "cell_phone")有沒(méi)有一種方法可以將這些合并為一個(gè)表單,或者我是否只需要?jiǎng)?chuàng)建一個(gè)表單并處理數(shù)據(jù)庫(kù)加載并保存自己?
查看完整描述

3 回答

?
明月笑刀無(wú)情

TA貢獻(xiàn)1828條經(jīng)驗(yàn) 獲得超4個(gè)贊

您可以只在一個(gè)<form>html元素的模板中顯示兩種形式。然后,只需在視圖中分別處理表單即可。您仍然可以使用form.save()而不需要處理數(shù)據(jù)庫(kù)加載和保存您自己。


在這種情況下,您不需要它,但是如果您要使用具有相同字段名的表單,請(qǐng)查看prefixdjango表單的kwarg。


查看完整回答
反對(duì) 回復(fù) 2019-12-12
?
江戶(hù)川亂折騰

TA貢獻(xiàn)1851條經(jīng)驗(yàn) 獲得超5個(gè)贊

我和erikbwork都有一個(gè)問(wèn)題,即一個(gè)模型只能包含在一個(gè)通用的基于類(lèi)的視圖中。我找到了類(lèi)似苗的類(lèi)似方法,但是更加模塊化。


我寫(xiě)了一個(gè)Mixin,因此您可以使用所有通用的基于類(lèi)的視圖。定義模型,字段,現(xiàn)在還定義child_model和child_field-然后可以將兩個(gè)模型的字段包裝在標(biāo)簽中,如Zach描述。


class ChildModelFormMixin: 

    ''' extends ModelFormMixin with the ability to include ChildModelForm '''

    child_model = ""

    child_fields = ()

    child_form_class = None


    def get_child_model(self):

        return self.child_model


    def get_child_fields(self):

        return self.child_fields


    def get_child_form(self):

        if not self.child_form_class:

            self.child_form_class = model_forms.modelform_factory(self.get_child_model(), fields=self.get_child_fields())

        return self.child_form_class(**self.get_form_kwargs())


    def get_context_data(self, **kwargs):

        if 'child_form' not in kwargs:

            kwargs['child_form'] = self.get_child_form()

        return super().get_context_data(**kwargs)


    def post(self, request, *args, **kwargs):

        form = self.get_form()

        child_form = self.get_child_form()


        # check if both forms are valid

        form_valid = form.is_valid()

        child_form_valid = child_form.is_valid()


        if form_valid and child_form_valid:

            return self.form_valid(form, child_form)

        else:

            return self.form_invalid(form)


    def form_valid(self, form, child_form):

        self.object = form.save()

        save_child_form = child_form.save(commit=False)

        save_child_form.course_key = self.object

        save_child_form.save()


        return HttpResponseRedirect(self.get_success_url())

用法示例:


class ConsumerRegistrationUpdateView(UpdateView):

    model = Registration

    fields = ('firstname', 'lastname',)

    child_model = ConsumerProfile

    child_fields = ('payment_token', 'cart',)

或使用ModelFormClass:


class ConsumerRegistrationUpdateView(UpdateView):

    model = Registration

    fields = ('firstname', 'lastname',)

    child_model = ConsumerProfile

    child_form_class = ConsumerProfileForm

做完了 希望能對(duì)某人有所幫助。


查看完整回答
反對(duì) 回復(fù) 2019-12-12
  • 3 回答
  • 0 關(guān)注
  • 1173 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢(xún)優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)