Home>

Views.py

from .models import visitor, Worker, Documentation
Def Visitor_Edit (Request, PK):
    Visitor= Get_Object_or_404 (Visitor, PK= PK)
    If Request.method== "POST":
    # Transfer an instance of Visitor as an instance form and while saving ...
        Form= Visitorform (Request.post, Instance= Visitor)
        PFORM= WORKERFORM (Request.post)
        Dform= DocumentationForm (Request.post)
        if form.is_valid () and pform.is_valid () and dform.is_valid ():
            Visitor= Form.Save (Commit= False)
            Worker= PForm.Save (Commit= False)
            Documentation= Dform.Save (Commit= False)
            # Visitor.Author= Request.user
            # Visitor.Published_Date= TimeZone.Now ()
            visitor.save ()
            worker.save ()
            Documentation.Save ()
            Return Redirect ('Visitor_Detail', PK= Visitor.pk)
    ELSE:
    # And when we open a form for editing:
        Form= VisitorForm (Instance= Visitor)
        PFORM= WORKERFORM ()
        Dform= DocumentationForm ()
    Return Render (Remark, 'BL /Visitor_Edit.html', {'Form': Form, 'PForm': PFORM, 'Dform': Dform})
Def Visitor_New (Request):
If Request.method== "POST":
    Form= Visitorform (Request.post, Prefix= 'Form')
    PFORM= WORKERFORM (Refix= 'PFORM')
    Dform= DocumentationForm (Request.post, Prefix= 'Dform')
    if form.is_valid () and pform.is_valid () and dform.is_valid ():
        Visitor= Form.Save (Commit= False)
        Worker= PForm.Save (Commit= False)
        Documentation= Dform.Save (Commit= False)
        # Visitor.Author= Request.user
        # Visitor.Published_Date= TimeZone.Now ()
        visitor.save ()
        worker.save ()
        Documentation.Save ()
        Return Redirect ('Visitor_Detail', PK= Visitor.pk)
ELSE:
    Form= VisitorForm (prefix= 'Form')
    PFORM= WORKERFORM (prefix= 'pForm')
    Dform= DocumentationForm (Prefix= 'Dform')
    Return Render (Remark, 'BL /Visitor_Edit.html', {'Form': Form, 'PForm': PFORM, 'Dform': Dform})

visitor_edit.html

{% block content%}
    ≪
H1 >
New Visitor <
/h1 >
    ≪
Form Method= "POST" class= "post-form" >
{% csrf_token%}
        {{Form.as_p}}
        {{pForm.as_p}}
        {{dform.as_p}}
        ≪
Button Type= "Submit" class= "Save BTN BTN-DEFAULT" >
Save <
/Button >
    ≪
/Form >
{% endblock%}

Forms.py

from Django Import Forms
from .models import Visitor, Worker, Documentation
Class VisitorForm (Forms.Modelform):
    Class Meta:
        Model= visitor
        Fields= ('name_visitor', 'Surname_visitor', 'Phone', 'Address', 'entry_time', 'Time_out')
Class Workerform (Forms.Modelform):
    Class Meta:
        Model= Worker.
        Fields= ('name_worker', 'surname_worker', 'position_worker')
Class DocumentationForm (Forms.Modelform):
    Class Meta:
        Model= Documentation
        fields= ('document_number', 'document_number')

When filling out the form, only fields are stored in the end, which relate to the Visitor model and, respectively, visitorform. Tell me, what to change to keep fields in all forms?