class Lugar(models.Model):
PAIS=‘pais’
ESTADO=‘estado’
PARROQUIA=‘parroquia’
MUNICIPIO=‘municipio’
TIPO_LUGAR=[
(PAIS,'pais'),
(MUNICIPIO,'municipio'),
(ESTADO,'estado'),
(PARROQUIA,'parroquia'),
]
lugar_id=models.AutoField(primary_key=True,verbose_name='identificador')
lugar_tipo=models.CharField(max_length=50,null=False,blank=False,choices=TIPO_LUGAR,verbose_name='Tipo de lugar')
lugar_nombre=models.CharField(max_length=50,null=False,blank=False,verbose_name='Nombre del lugar')
fk_lugar_lugar=models.ForeignKey('Lugar',on_delete=models.CASCADE,null=True,blank=True,verbose_name='Lugar Origen')
I have this model called place, in it we have an attribute called place_type that stores (‘state’, ‘parish’, ‘country’, ‘municipality’) . Some models are related to this model, this relation is given only with the lowest key of Place, that is a parish.
I am using limit choice to avoid that from the admin site, a state or a municipality is chosen, as here:
fk_ron_lugar=models.ForeignKey(‘Lugar’,on_delete=models.CASCADE,related_name=‘ron_lugar’,verbose_name=‘Origen’,limit_choices_to={‘lugar_tipo’:‘parroquia’})
but this is generating that all the parishes presented in the model Place appear to me.
To avoid this I want to create in the form of the model that is related to place, a kind of filtering that works as follows:
I would like to create three fields, one called state, that loads directly all the states present in the table place.
Another one called municipality, where dynamically load the child municipalities of the selected state and finally one called parish where load the child parishes of the previously selected municipality, dynamically. This is to avoid loading all the parishes of all the municipalities and to have a better order in the selection.