Very good to everyone. I have a problem when displaying elements in a form in Django. I have 2 models, one called Recurso
and another called Sesion
, which are related in such a way that a resource can have several sessions available, with the particularity that this association "Resource - Sessions" only occurs in the resources that be of the 'Per Sessions' type.
What I have to achieve and I can not find anywhere on the internet is that when the value of the selector of the field "typeResource" of the form changes, the widget of choice of sessions is shown. If not, no.
Code of the models:
class Sesion(models.Model):
horaInicial = models.TimeField()
horaFinal = models.TimeField()
class Meta:
verbose_name = "Sesión"
verbose_name_plural = "Sesiones"
def __str__(self):
return "{}".format(self.horaInicial)
class Recurso(models.Model):
SESIONES = 'SE'
COLA = 'CL'
RECURSO_TIPOS = (
(SESIONES, 'Por Sesiones'),
(COLA, 'En Cola'),
)
nombre = models.CharField(max_length=60)
descripcion = models.TextField()
tipoRecurso = models.CharField(max_length=2, choices = RECURSO_TIPOS)
division = models.ForeignKey(Division, related_name = "divisionRecurso")
precioA = models.FloatField()
precioB = models.FloatField()
precioC = models.FloatField()
imagen = models.ImageField(upload_to='media', null = True)
sesiones = models.ManyToManyField(Sesion, blank=True)
class Meta:
verbose_name = "Recursos"
verbose_name_plural = "Recursos"
def __str__(self):
return "{}".format(self.nombre)
Form code:
class RecursoUpdateForm(forms.ModelForm):
class Meta:
model = Recurso
fields = ['nombre', 'descripcion', 'tipoRecurso', 'precioA', 'precioB', 'precioC', 'imagen', 'sesiones']
widgets = {
'sesiones': FilteredSelectMultiple("Sesiones", is_stacked=False),
}
def __init__(self, *args, **kwargs):
super(RecursoUpdateForm, self).__init__(*args, **kwargs)
I hope you can help me, thank you very much in advance.