ModelForm with OneToOneField in Django

前端 未结 2 1459
情深已故
情深已故 2020-12-02 15:42

I have two models in Django that are related with a OneToOneField (PrinterProfile and PrinterAdress). I am trying to do a form with

2条回答
  •  一生所求
    2020-12-02 16:31

    You have to create second form for PrinterAddress and handle both forms in you view:

    if all((profile_form.is_valid(), address_form.is_valid())):
        profile = profile_form.save()
        address = address_form.save(commit=False)
        address.printer_profile = profile
        address.save()
    

    Of course in the template you need to show both forms under one

    tag :-)

    
        {% csrf_token %}
        {{ profile_form }}
        {{ address_form }}
    
    

提交回复
热议问题