Saving a model to another model OneToMany

Hi guys. I got 3 models:

class Patient(models.Model):
         NameandLastName =models.CharField(max_length = 50)

class Consult(models.Model):
         Simptoms =models.CharField(max_length = 100)
         Patient = models.ForeignKey(Patient,  on_delete = models.CASCADE)
class Hospitalization(models.Model):
         hospitalization_section=models.CharField(choices = section, max_length = 100)
         Patient = models.ForeignKey(Patient,  on_delete = models.CASCADE)

I want to apply “Consults and Hospitalization” to each/specific Patient. How can I do that?
I use CreateView/UpdateView(…) to manage patients profiles, where I want to print into a table the Consults for each Patient(where i can access them to Edit/Delete…). for Patient profile, where I access the form for adding a Hospitalization:

class PatientUpdateView(LoginRequiredMixin,  UpdateView):
    model = Patient
    template_name = 'Manage/PageAddHos.html'
    form_class = forms.FormAddHost# I create in a form, using form|crispy

    def form_valid(self, form):
        form.instance.autor = self.request.user
        messages.success(self.request, f"...")
        return super().form_valid(form)

The problem: I want to access my Add_Consult form(which I access from Patient"DetailView"), where Name, Personal_ID, Gender(…) to autocomplete for that Patient.
Thank you for your time!

I’m trying to decode your question. Here is what I got:
You want to automatically enter values into the Consult and Hospitalization models after creating/updating the Patient?

Not realy.
I want to add consultations & hospitalizations, and these must be assigned per patient.
Now I get this error: DETAIL: Failing row contains (6, 2022-06-18 09: 50: 32.722841 + 00, 2022-06-18 00: 00: 00 + 00, NE, something, something, null).
I know that in the last field, “null” must be the patient’s id, but I don’t know how to get it.
#this is my view for add a Consult, but when I submit the form, I got that error:


class Consult(LoginRequiredMixin, CreateView):
model = Consult
template_name = 'Manage/ConsultAdd.html'
form_class = forms.FormConsultAdd

def form_valid(self, form):
    form.instance.autor = self.request.user
    messages.success(self.request, f"smth")
    return super().form_valid(form)

In this general case, your link from the Patient page to this Consult page needs to pass the Patient id as a url parameter, which you can then assign as the patient_id field of the Consult model when being created.

Side note: I suggest you adopt the standard Python and Django code conventions - it’s going to help you a lot in the long run. Specifically in this case, class names such as Consult are properly capitalized. However, the names of the fields (symptoms and patient should not be.) Likewise, you want to be consistent with your naming patterns. You use the “word_word” style for hospitalization_section (good), but then you have “NameandLastName” instead of either name_and_last_name (if that’s what you want) or possibly full_name.

Thanks for the response. I actually replace the names of variables, because these are in my native language.



path('Add_Profile_Patient/new/', Add_Profil_PatientCreateView.as_view(), name='Manage-Add_Profile_Patient'),
	path('View_Profile_Patient/<int:pk>/',View_Profile_PatientDetailView.as_view(), name='Manage-View_Profile_Patient'),
	path('Update_Profile_Patient/<int:pk>/update/', Update_Profile_PatientUpdateView.as_view(), name='Manage-View_Profile_Patient'),
	path('Delete_Profile_Patient/<int:pk>/delete/', Delete_Profile_PatientDeleteView.as_view(), name='Manage-View_Profile_Patient'),
	path('Add_Hospitalization/new', Add_HospitalizationCreateView.as_view(), name='Manage-Add_Hospitalization'),


This is going into which table/model? Its hard to know exactly what you are trying to do when relevant parts of your code are missing. Otherwise, can you share the line preceeding that error message?