My models.py
# -*- encoding: utf-8 -*-
from django.db import models
from general.models import BaseUnique, BaseTitle, Empresa, Arl
from recurso_humano.models import Persona, Sucursal
# ===================== HEALTH BASIC =========================
TIPO_DOCUMENTO = (
(1, 'Cédula'),
(2, 'Nit'),
)
class Especialidad(BaseUnique):
pass
class ProveedorSalud(models.Model):
tipo_documento = models.IntegerField(choices=TIPO_DOCUMENTO, verbose_name="tipo de Identificación")
numero_documento = models.BigIntegerField(unique=True, verbose_name='número de Identificación')
nombre_completo = models.CharField(max_length=255, verbose_name='Nombre Completo')
telefonos = models.CharField(max_length=255, verbose_name='Teléfono(s)')
especialidades = models.ManyToManyField(Especialidad, blank=True, through='ProveedorEspecialidad', related_name="esp")
def __str__(self):
return ("%s"%(self.nombre_completo)).strip() or "-"
class ProveedorEspecialidad(models.Model):
proveedor = models.ForeignKey(ProveedorSalud)
especialidad = models.ForeignKey(Especialidad)
class DatosExamenMedico(models.Model):
codigo = models.CharField(max_length=255)
nombre = models.CharField(max_length=255, verbose_name="Nombre Exámen")
proveedor = models.ForeignKey(ProveedorSalud)
especialidad = models.ForeignKey(Especialidad)
precio_de_proveedor = models.IntegerField(verbose_name="Precio de Proveedor")
precio_a_cliente = models.IntegerField(verbose_name="Precio a Cliente")
class Meta:
unique_together = (("codigo", "nombre"),)
def __str__(self):
return ("%s (%s)"%(self.codigo, self.nombre)).strip() or "-"
My initial forms.py
# -*- encoding: utf-8 -*-
import random, string, os
from django import forms
from django.forms import ModelForm
from django_select2.forms import Select2MultipleWidget, Select2Widget
from django.contrib.auth.models import User
from django_select2.forms import ModelSelect2Widget, Select2Widget
from gestion_salud.models import *
from general.models import Empresa, Arl, ActividadEconomica, Empresa_ActividadEconomica
class DatosExamenMedicoForm(forms.ModelForm):
class Meta:
model = DatosExamenMedico
exclude = ()
widgets = {
}
class EspecialidadForm(forms.ModelForm):
class Meta:
model = Especialidad
exclude = ()
class ProveedorSaludForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super(ProveedorSaludForm, self).__init__(*args, **kwargs)
self.fields['especialidades'].queryset = Especialidad.objects.all()
class Meta:
model = ProveedorSalud
exclude = ()
widgets = {
'especialidades': Select2MultipleWidget()
}
Part of my views.py
# -*- encoding: utf-8 -*-
from django.http import Http404
from django.shortcuts import redirect, render
from django.contrib.auth.decorators import login_required
from gestion_salud.forms import *
from recurso_humano.forms import PersonaForm, PersonaForm_Basico, CedulaForm
from recurso_humano.models import Persona
from recurso_humano.views.gestion_humana import calcular_tipo
@login_required
def examen_medico_crear(request):
if request.method == 'POST':
form = DatosExamenMedicoForm(request.POST, request.FILES)
if form.is_valid():
objeto = form.save(commit=False)
objeto.save()
request.session['em_creada'] = True
return redirect('matriz_em')
else :
form = DatosExamenMedico_crear_Form()
return render(request, 'em_crear__salud.html', {
'form': form,
})
It turns out that if I send the default form for DatosExamenMedico
I can select any ProveedorSalud
and any Especialidad
, but the idea is not that, but first select the provider and automatically in the field Especialidad
filtren only the specialties of that ProveedorSalud
.
I have been told to use ajax, but I do not know how to find the info I need, neither in Spanish nor in English, an example would be very helpful, even if it is from countries-cities.