I have a Django form that receives entries from the users with information on a surgical procedure. Each Procedure will have only one (surgical) Technique and only one Diagnosis. Each Technique may be related to a limited number of Diagnosis, and each Diagnosis may be used on different Techniques. I want to limit which Diagnosis appear of the form based on which Technique the user selected previously on the form.
I tried using smart_selects ChainedManyToMany field with relative success, but it enable multiple Diagnosis to be selected, I only want to have one.
I`m also using DAL for autocompleting the Technique (over 1,6k options) as the user types.
My models:
# The "Technique" model
class Sigtap(models.Model):
codigo = models.CharField(max_length=10)
descricao = models.CharField(max_length=175, default='')
# The "Diagnosis" model
class Cid10(models.Model):
codigo = models.CharField(max_length=4)
descricao = models.CharField(max_length=270, default='')
sigtap_compativel = models.ManyToManyField(Sigtap, blank=True)
# The "Surgical Procedure" model
class Cirurgia(models.Model):
paciente = models.PositiveIntegerField(verbose_name='Número do prontuário')
data = models.DateField(verbose_name='Data de realização')
procedimento = models.ForeignKey(Sigtap, on_delete=models.CASCADE,
verbose_name='Código do procedimento (SIGTAP)')
cid = ChainedManyToManyField(
Cid10, horizontal=True, chained_field='procedimento', chained_model_field='sigtap_compativel', auto_choose=True, verbose_name='CID-10')
My forms:
class CirurgiaModelForm(forms.ModelForm):
class Meta:
model = Cirurgia
fields = ['paciente', 'data', 'procedimento', 'cid']
widgets = {
'procedimento': autocomplete.ModelSelect2(url='sigtap-autocomplete'),
}
How can I get the form field to show only the Diagnosis related to the Technique selected and allow only one option?