Konstantin18ko
@Konstantin18ko
Стоматолог

Как реализовать каскадное сохранение?

Есть 3 модели, ПАЦИЕНТ, ПОЛИС, ПАСПОРТ.
Code1
from django.db import models

# Create your models here.

class Patient (models.Model):
    surName = models.CharField(max_length=100)
    firstName = models.CharField(max_length=100)
    secondName = models.CharField(max_length=100)
    dateOfBirth = models.DateField()

class InsurancePolicy (models.Model):
    id_patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
    surName = models.CharField(max_length=100, blank=True, null=True)
    firstName = models.CharField(max_length=100, blank=True, null=True)
    secondName = models.CharField(max_length=100, blank=True, null=True)
    dateOfBirth = models.DateField(blank=True, null=True)
    validity = models.DateField(blank=True, null=True)
    insuranceDate = models.DateField(blank=True, null=True)
    insuranceOffice = models.TextField(max_length=250, blank=True, null=True)

class Passport (models.Model):
    id_patient = models.ForeignKey(Patient, on_delete=models.CASCADE)
    serialPassport = models.CharField(max_length=10, blank=True, null=True)
    numberPassport = models.CharField(max_length=10, blank=True, null=True)
    passportOffice = models.TextField(blank=True, null=True)
    passportEmit = models.DateField(blank=True, null=True)
    passportOfficeCode = models.CharField(max_length=30, blank=True, null=True)
    surName = models.CharField(max_length=100, blank=True, null=True)
    firstName = models.CharField(max_length=100, blank=True, null=True)
    secondName = models.CharField(max_length=100, blank=True, null=True)
    dateOfBirth = models.DateField(blank=True, null=True)
    region = models.CharField(max_length=100, blank=True, null=True)
    station = models.CharField(max_length=100, blank=True, null=True)
    district = models.CharField(max_length=100, blank=True, null=True)
    street = models.CharField(max_length=100, blank=True, null=True)
    home = models.CharField(max_length=30, blank=True, null=True)
    housing = models.CharField(max_length=30, blank=True, null=True)
    flat = models.CharField(max_length=30, blank=True, null=True)

Есть три формы, на основе моделей:
Code2
from django import forms
from .models import *


class addPatient(forms.ModelForm):
    surName = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light text-dark', 'style': 'text-align: left',
               'v-model': 'surName', '@keyup': 'checkPatient'}))
    firstName = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light text-dark', 'style': 'text-align: left',
               'v-model': 'firstName', '@keyup': 'checkPatient'}))
    secondName = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light text-dark', 'style': 'text-align: left',
               'v-model': 'secondName', '@keyup': 'checkPatient'}))
    dateOfBirth = forms.DateField(widget=forms.DateInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light text-dark', 'style': 'text-align: left',
               'v-model': 'dateOfBirth', '@keyup': 'checkPatient', 'type': 'date'}))

    class Meta:
        model = Patient
        exclude = ['']


class addInsurancePolicy(forms.ModelForm):
    numberInsurancePolicy = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light col-4 text-dark', 'style': 'text-align: left'}), required=False)
    surName = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light text-dark', 'style': 'text-align: left'}), required=False)
    firstName = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light text-dark', 'style': 'text-align: left'}), required=False)
    secondName = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light text-dark', 'style': 'text-align: left'}), required=False)
    dateOfBirth = forms.DateField(widget=forms.DateInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light text-dark', 'style': 'text-align: left'}), required=False)
    validity = forms.DateField(widget=forms.DateInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light text-dark', 'style': 'text-align: left',
               'type': 'date'}), required=False)
    insuranceDate = forms.DateField(widget=forms.DateInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light text-dark', 'style': 'text-align: left',
               'type': 'date'}), required=False)
    insuranceOffice = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light col-3 text-dark', 'style': 'text-align: left'}), required=False)

    class Meta:
        model = InsurancePolicy
        fields = '__all__'
        exclude = ['']


class addPassport(forms.ModelForm):
    serialPassport = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light text-dark', 'style': 'text-align: left'}), required=False)
    numberPassport = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light text-dark', 'style': 'text-align: left'}), required=False)
    passportOffice = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light col-5 text-dark', 'style': 'text-align: left'}), required=False)
    passportOfficeCode = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light text-dark', 'style': 'text-align: left'}), required=False)
    surName = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light text-dark', 'style': 'text-align: left'}), required=False)
    firstName = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light text-dark', 'style': 'text-align: left'}), required=False)
    secondName = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light text-dark', 'style': 'text-align: left'}), required=False)
    dateOfBirth = forms.DateField(widget=forms.DateInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light text-dark', 'style': 'text-align: left'}), required=False)
    region = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light col-4 text-dark', 'style': 'text-align: left'}), required=False)
    station = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light col-2 text-dark', 'style': 'text-align: left'}), required=False)
    district = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light col-2 text-dark', 'style': 'text-align: left'}), required=False)
    street = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light text-dark', 'style': 'text-align: left'}), required=False)
    home = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light text-dark', 'style': 'text-align: left'}), required=False)
    housing = forms.DateField(widget=forms.DateInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light text-dark', 'style': 'text-align: left',
               'type': 'date'}), required=False)
    flat = forms.CharField(widget=forms.TextInput(
        attrs={'class': 'btn btn-sm btn-outline-primary bg-light text-dark', 'style': 'text-align: left'}), required=False)

    class Meta:
        model = Passport
        fields = '__all__'
        exclude = ['']


Как сохранить форму с взаимосвязью?
Code3
from django.shortcuts import render
from .forms import *

# Create your views here.
def index (request):
    formAddPatient = addPatient(request.POST or None)
    formAddInsurancePolicy = addInsurancePolicy(request.POST or None)
    formAddPassport = addPassport(request.POST or None)
    if request.method == 'POST' or formAddPatient.is_valid() or formAddInsurancePolicy.is_valid() or formAddPassport.is_valid():
        print(formAddPatient)
        print(formAddInsurancePolicy)
        print(formAddPassport)
        print(formAddPatient.cleaned_data)
        print(formAddInsurancePolicy.cleaned_data)
        print(formAddPassport.cleaned_data)
        dataAddPatient = formAddPatient.save()

    return render(request, 'patient/index.html', locals())
  • Вопрос задан
  • 180 просмотров
Пригласить эксперта
Ответы на вопрос 1
@JonGalt
Сначала сохраняете форму пациента
dataAddPatient = formAddPatient.save()
Потом просто создаете InsurancePolicy и Passport не через сохранение формы, а вытаскиваете данные cleaned_data из сооветствующей формы и создаете новую запись.
surName = formAddInsurancePolicy.cleaned_data['surName']
...
newPolicy = InsurancePolicy(surName=surName, ... , id_patient=dataAddPatient)
newPolicy.save()
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы