class Paciente(models.Model):
expediente = models.CharField(primary_key=True,max_length=10)
nombre = models.CharField(max_length=100)
paterno = models.CharField(max_length=100)
materno = models.CharField(max_length=100)
estado = models.ForeignKey(Estado,related_name='Estado_set')
municipio = models.ForeignKey(Municipio,related_name='Municipio_set')`enter code here`
localidad = models.ForeignKey(Localidad,related_name='Localidad_set')
calle = models.CharField(max_length=100)
noExterior = models.IntegerField(max_length=4)
noInterior = models.CharField(max_length=4,blank=True, null=True)
...
def __unicode__(self):
name = "%s - %s %s %s"%(self.expediente,self.nombre,self.paterno,self.materno)
return name
class FamParentesco(models.Model):
GENERO = (
('F', 'Femenino'),
('M', 'Masculino')
)
paciente = models.ForeignKey(Paciente)
nombre = models.CharField(max_length=100)
edad = models.IntegerField(max_length=2,blank=True, null=True)
sexo = models.CharField(max_length=1, choices=GENERO)
parentesco = models.ForeignKey(Parentesco)
escolaridad = models.ForeignKey(Escolaridad,blank=True, null=True)
ocupacion = models.ForeignKey(Ocupacion,blank=True, null=True)
estadoCivil = models.ForeignKey(EstadoCivil,blank=True, null=True)
ingreso = models.DecimalField(max_digits=5,decimal_places=2,blank=True, null=True)
def __unicode__(self):
return self.nombre
class EgresoMensual(models.Model):
paciente = models.OneToOneField(Paciente, unique=True, related_name='egresomensual')
renta = models.DecimalField(max_digits=8,decimal_places=2,blank=True, null=True)
alimentacion = models.DecimalField(max_digits=8,decimal_places=2,blank=True, null=True)
luzaguacomb = models.DecimalField(max_digits=8,decimal_places=2,blank=True, null=True)
educacion = models.DecimalField(max_digits=8,decimal_places=2,blank=True, null=True)
vestidop = models.DecimalField(max_digits=8,decimal_places=2,blank=True, null=True)
pasajes = models.DecimalField(max_digits=8,decimal_places=2,blank=True, null=True)
abonos = models.DecimalField(max_digits=8,decimal_places=2,blank=True, null=True)
otros = models.DecimalField(max_digits=8,decimal_places=2,blank=True, null=True)
total = models.DecimalField(max_digits=8,decimal_places=2,blank=True, null=True)
class OtrosIngresos(models.Model):
paciente = models.OneToOneField(Paciente)
propiedades = models.BooleanField()
detalleprop = models.CharField(max_length=100)
rentas = models.BooleanField()
negocio = models.BooleanField()
detallearticulos = models.CharField(max_length=100)
class Vivienda(models.Model):
paciente = models.OneToOneField(Paciente)
tipo = models.CharField(max_length=100)
piso = models.BooleanField()
techo = models.BooleanField()
ventana = models.BooleanField()
habitaciones = models.IntegerField()
agua = models.BooleanField()
excreta = models.BooleanField()
basura = models.BooleanField()
energia = models.CharField(max_length=100)
hacinamiento = models.BooleanField()
promiscuidad = models.BooleanField()
class Observaciones(models.Model):
paciente = models.OneToOneField(Paciente)
observaciones = models.TextField()
from django import forms
from django.forms import ModelForm
from django.forms.models import inlineformset_factory
from .models import EgresoMensual, Observaciones, OtrosIngresos, Paciente, FamParentesco, Vivienda
class PacienteForm(forms.ModelForm):
class Meta:
model = Paciente
ParentescoFormSet = inlineformset_factory(Paciente, FamParentesco)
class EgresoMensualForm(forms.ModelForm):
class Meta:
model = EgresoMensual
exclude = ('paciente')
class ObservacionesForm(forms.ModelForm):
class Meta:
model = Observaciones
class OtrosIngresosForm(forms.ModelForm):
class Meta:
model = OtrosIngresos
class ViviendaForm(forms.ModelForm):
class Meta:
model = Vivienda
from django.http import HttpResponseRedirect
from django.views.generic import CreateView
from .models import Paciente, FamParentesco
from .forms import PacienteForm, ParentescoFormSet, ObservacionesForm, ViviendaForm, OtrosIngresosForm, EgresoMensualForm
class PatientCreateView(CreateView):
template_name = 'add_expediente.html'
model = Paciente
form_class = PacienteForm
success_url = 'success/'
def get(self, request, *args, **kwargs):
self.object = None
form_class = self.get_form_class()
form = self.get_form_class()
family_form = ParentescoFormSet()
egreso_form = EgresoMensualForm()
return self.render_to_response(
self.get_context_data(form=form,
family_form=family_form,
egreso_form = egreso_form))
def post(self, request, *args, **kwargs):
self.object = None
form_class = self.get_form_class()
form = self.get_form(form_class)
family_form = ParentescoFormSet(self.request.POST)
egreso_form = EgresoMensualForm(self.request.POST, instance=request.paciente.egresomensual)
if (form.is_valid() and family_form.is_valid()):
return self.form_valid(form, family_form, egreso_form)
else:
return self.form_invalid(form, family_form, egreso_form)
def form_valid(self, form, family_form, egreso_form):
self.object = form.save()
family_form.instance = self.object
egreso_form.instance = self.object
family_form.save()
return HttpResponseRedirect(self.get_success_url())
def form_invalid(self, form, family_form, egreso_form):
return self.render_to_response(
self.get_context_data(form=form,
family_form=family_form,
egreso_form=egreso_form))
我可以使用OneToOneField关系在我的视图中显示表单吗?
我试图与EgresoMesual Pacientee建立关系,通过GET方法创建表格
但要通过POST方法并关联表格EgresoMensualForm
egreso_form = EgresoMensualForm(self.request.POST,instance = request.paciente.egresomensual)
我标记错误:
/ ece / file /
中的AttributeError'WSGIRequest'对象没有属性'patient'
如何创建一个采用模型的单一表格:EgresoMensual,OtrosIngresos,Holiday Observations