2017-02-02 6 views
0

ここでは非常に明白なものがありませんが、これを動作させることはできません。Djangoは複数のmodelformをビューに保存します

2つのモデル(組織と住所)、2つのフォーム(各モデルに1つ)、1つのビュー(組織を住所を子として保存する場所)があります。

モデル:

class Address(models.Model): 
    address = models.CharField(max_length=255, verbose_name=_("Address")) 
    postal_code = models.CharField(max_length=20, verbose_name=_("Postal_code")) 
    city = models.CharField(max_length=255, verbose_name=_("City")) 


class Organization(models.Model): 
    name = models.CharField(max_length=100, verbose_name=_("Name")) 
    address = models.OneToOneField(Address, on_delete=models.CASCADE, verbose_name=_("Address")) 

    owner = models.ForeignKey("users.User", related_name="organizations", verbose_name=_("Owner")) 

フォーム:

class AddressForm(forms.ModelForm): 
    class Meta: 
     model = Address 
     fields = ["address", "postal_code", "city"] 

    def __init__(self, *args, **kwargs): 
     super(AddressForm, self).__init__(*args, **kwargs) 
     self.helper = FormHelper(self) 
     self.helper.form_tag = False 
     self.helper.disable_csrf = True 


class OrganizationForm(forms.ModelForm): 
    class Meta: 
     model = Organization 
     fields = ["name", ] 

    def __init__(self, *args, **kwargs): 
     super(OrganizationForm, self).__init__(*args, **kwargs) 
     self.helper = FormHelper(self) 
     self.helper.form_tag = False 
     self.helper.disable_csrf = True 

そして最後にビュー

class OrganizationCreateView(LoginRequiredMixin, TemplateView): 
    template_name = "organizations/organization_form.html" 

    def get_organization_form(self, data=None): 
     return OrganizationForm(data) 

    def get_address_form(self, data=None): 
     return AddressForm(data) 

    def get(self, request, *args, **kwargs): 
     ctx = self.get_context_data(organization_form=OrganizationForm(), 
            address_form=AddressForm()) 
     return self.render_to_response(ctx) 

    def post(self, request, *args, **kwargs): 
     organization_form = self.get_organization_form(data=request.POST) 
     address_form = self.get_address_form(data=request.POST) 

     if organization_form.is_valid() and address_form.is_valid(): 
      return self.forms_valid(organization_form, address_form) 
     return self.forms_invalid(organization_form, address_form) 

    def forms_valid(self, organization_form, address_form): 
     address = address_form 
     address.country = "DE" 
     address.save() 

     organization = organization_form 
     organization.save(commit=False) 

     organization.owner = self.request.user 
     organization.address = address 
     organization.save() 

     return redirect("list") 

    def forms_invalid(self, organization_form, address_form): 
     ctx = self.get_context_data(organization_form=self.get_organization_form(organization_form), 
            address_form=self.get_address_form(address_form)) 
     return self.render_to_response(ctx) 

機関(commit = Falseを持つ)の隣だから私は、アドレスを保存しません、組織にアドレスを追加し、組織を保存してください。例外:例外

Exception Type: IntegrityError at /organizations/create/ 
Exception Value: null value in column "address_id" violates not-null constraint 
DETAIL: Failing row contains (9, slkdfjlsdfk, lkdjflskdjflsd, , llksjjdlfkjsdlfkjsldkfj, f, null, null, null, null, null, null, null, null, null, null). 

私がすでに述べたように、それはおそらく非常に明白ですが、それを見つけることはできません。誰かがここで私を助けることができますか?

答えて

2

フォームからモデルインスタンスを作成する方法について、少し混乱しています。インスタンスはform.save()への呼び出しから返されます。それはあなたがアドレスとユーザーを割り当てる必要があるインスタンスです。

def forms_valid(self, organization_form, address_form): 
    address = address_form.save(commit=False) 
    address.country = "DE" 
    address.save() 

    organization = organization_form.save(commit=False) 
    organization.owner = self.request.user 
    organization.address = address 
    organization.save() 

    return redirect("list") 
+0

それでした。ありがとう。 –