[Answered ]-Prepopulate Django ModelForm With all records

1πŸ‘

βœ…

I don’t think you can pass a queryset, runninghours by using initial. That takes a dictionary as argument. What you might be able to do is use the queryset in making the form.

# forms.py

class RunningHoursModelForm(ModelForm):

    def __init__(self,company,*args,**kwargs):
        super (RunningHoursModelForm,self ).__init__(*args,**kwargs)
        self.fields['the_field_you_want'].queryset = RunningHours.objects.all()

    class Meta:
        model = RunningHours

If you want to populate the form with an instance of RunningHours, you can just do

    particularRunningHours = RunningHours.objects.get(name=name)
    form_populated =  RunningHoursModelForm(initial={
        'current_runninghours': particularRunningHours.current_runninghours,
        ...
        })
πŸ‘€raphael

Leave a comment