1
TIMESLOT_LIST = (
        (5, '09:00 – 09:30'),
        (9, '09:30 – 10:00'),
        (2, '10:00 – 10:30'),
    )
    
class AvailabilitiesForm(forms.Form):
    time_slot = forms.ModelMultipleChoiceField(queryset = None, widget=forms.CheckboxSelectMultiple)

    def __init__(self, *args, **kwargs):
        date = kwargs.pop('date')
        super(AvailabilitiesForm, self).__init__(*args, **kwargs)
        #choices = getChoices(letter)
        self.fields['time_slot'].queryset = Appointment.objects.filter(date = date).values_list('time_slot', flat = True)

The view :

@login_required
def chooseAvailabilities(request):
    date_7 = (datetime.date.today() + datetime.timedelta(days=7)).strftime("%Y-%m-%d")
    appointment_7 = Appointment.objects.filter(date = date_7).all()

return render(request, 'panel/choose_availabilities.html', {'tomorrow' : appointment_7})

The models.py :

class Appointment(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, null = True, on_delete = models.CASCADE)
    student = models.ForeignKey(Student, null=True, blank = True, on_delete=models.CASCADE)
    date = models.DateField(null = True)
    time_slot = models.IntegerField(default = 0, choices= TIMESLOT_LIST)

This displays :

enter image description here

I want :

enter image description here

How can I use the TIMESLOT_LIST to display the string "9h00 - 9h30" and not 5 ?

While passing through, how can I can withdraw the bullet points ?

anthonya
  • 565
  • 2
  • 6
  • 15
  • Does this answer your question? [django: customizing display of ModelMultipleChoiceField](https://stackoverflow.com/questions/3695754/django-customizing-display-of-modelmultiplechoicefield) – Brian Destura Aug 12 '21 at 02:54
  • Thank you. It's not answering my question because my Form class is not overriding a ModelForm class in which I could use the object for the label_from_instance function. In my Form class, I get only a queryset and not the object. Can I use label_from_instance for a queryset and not an object ? – anthonya Aug 12 '21 at 10:56

1 Answers1

1

you can do the following:-

from django.utils.translation import gettext_lazy as _

class Appointment(models.Model):

    class TimeSlot(models.TextChoices):
        FIRST_T = '5', _('9:00 - 09:30')
        SECOND_T = '9', _('09:30 - 10:00')
        THIRD_T = '2', _('10:00 - 10:30')

    time_slot = models.CharField(max_length=1,choices=TimeSlot.choices,default=TimeSlot.FIRST_T)


class AvailabilitiesForm(forms.Form):
    time_slot = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple)

    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)
        d1 = kwargs.pop('date')
        ava_time = []

        for i in Appointment.objects.filter(date=d1):
            for x in Appointment.TimeSlot.choices:
                if x[0] == i.time_slot:
                    ava_time.append(x)

        self.fields['time_slot'].choices = ava_time
Neeraj
  • 783
  • 5
  • 8
  • What about the date in kwargs that I give in my view to the form ? – anthonya Aug 12 '21 at 20:47
  • could you please are your Appointment model because as of now I am not clear how are the dates and time slots linked. from your question I thought only problem was the display and bullets. – Neeraj Aug 13 '21 at 03:42
  • I added it. In my view, I give to the Form a date in which I want the hours to be displayed. – anthonya Aug 13 '21 at 10:29
  • made the required changes for date. please check. – Neeraj Aug 13 '21 at 17:26
  • Thank you, it helped me a lot. You have just to invert like this in the Form : class AvailabilitiesForm(forms.Form): time_slot = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple) def __init__(self,*args,**kwargs): date = kwargs.pop('date') super().__init__(*args,**kwargs) ava_value_timeslots = ((key, val) for key, val in dict(Appointment.TimeSlot.choices).items() if int(key) not in list(Appointment.objects.filter(date = date).values_list('time_slot', flat = True))) self.fields['time_slot'].choices = ava_value_timeslots – anthonya Aug 15 '21 at 16:12