0

I need a design recommendation about Django formset with multiple choicefields(select) which have same datasource.

in my project I have simply:

class Event(models.Model):
    name= models.Charfield()


class Schedule(models.Model):
    day = models.CharField(max_length=10)
    hour = models.CharField(max_length=10)
    event = models.ForeignKey(Event, on_delete=models.DO_NOTHING, 
             related_name="event")

class FormSchedule(forms.ModelForm):
    class Meta:
        model = Schedule
        fields = '__all__'


def schedule(request):
     
        if request.method == 'POST':
            .....

        else:
            event_queryset = Event.objects.all()
            choice = maketuple(event_queryset)

            form_size = 24 * 7  # weekly schedule

            formset_ = formset_factory(
                    FormSchedule, extra=168)
                formset = formset_()
               
                for form in formset:
                  
                    form.fields['event'].choices = choice
                    
                    
    
        return render(request, 'core/schedule.html','formset': formset})
 

it works in runitme without error. I have html page with 168 selects and each select has 50 options. the problem is It takes more than 15 seconds loading page. is there any way to optimize this process

thanks a lot,

  • Does this answer your question? [Django Inline for ManyToMany generate duplicate queries](https://stackoverflow.com/questions/40665770/django-inline-for-manytomany-generate-duplicate-queries) – Abdul Aziz Barkat May 15 '21 at 15:57
  • Specifically see the [answer by@isobolev](https://stackoverflow.com/a/44932400/14991864), you will need to adjust it a bit, since it is made for inline formsets (mostly changing from where you inherit). – Abdul Aziz Barkat May 15 '21 at 15:58

0 Answers0