Build MultipleChoiceField form based on view request with Django

I have a table named Appointment with :

  • timeslot id ;
  • student id;
  • teacher id ;
  • date ;
  • timeslot (integer) ;

I would like to create multiple rows in the table TimeSlot based on MultipleChoiceField form. But the choice of slot at a certain date is depending of the value in the table. For example, if there is a timeslot at a certain date, the choice for this timeslot to this date will not be displaying to the client.

This is my code :

TIMESLOT_LIST = (
        (0, '09:00 – 09:30'),
        (1, '09:30 – 10:00'),
    )

class ChooseAvailabilities(forms.Form):
    timeslot= forms.MultipleChoiceField(
        required=False,
        widget=CheckboxSelectMultiple,
        choices=TIMESLOT_LIST
        )

class Appointment(models.Model):
    teacher = models.ForeignKey(Teacher, null = True, on_delete = models.CASCADE)
    student = models.ForeignKey(Student, null=True, blank = True, on_delete=models.CASCADE)
    date = models.DateField(null = True)
    timeslot = models.IntegerField(null = True)

In the view, I want to call the ChooseAvailabilties form with a certain date in parameter so that the form is checking if there are overlapping date or timeslot in the Appointment table to not show the timeslot in which there are overlappings but only available timeslots to this date.

Something like where I can choose the timeslots. Then the selected timeslots are stored in the Appointment table (5 maximum) :

Back to Top