Initial data doesn't go complelely in the modelformset

12 views
Skip to first unread message

VISHESH MANGLA

unread,
Jun 13, 2021, 2:39:49 PM6/13/21
to Django users
in line 38 i have that print statement, it's output is at the top , I dont know why that initial data is not going into that model formset.
actually one data the PAN_ID actually went into the modelform, but the checkbox's "checked" didnt. You can see the html of the formset and the checkbox field doesnt has a checked.


Code in paste 

GET
 <input type="hidden" name="form-TOTAL_FORMS" value="1" id="id_form-TOTAL_FORMS"><input type="hidden" name="form-INITIAL_FORMS" value="1" id="id_form-INITIAL_FORMS"><input type="hidden" name="form-MIN_NUM_FORMS" value="0" id="id_form-MIN_NUM_FORMS"><input type="hidden" name="form-MAX_NUM_FORMS" value="1000" id="id_form-MAX_NUM_FORMS">
<tr><th><label for="id_form-0-print_report_check">Print report check:</label></th><td><input type="checkbox" name="form-0-print_report_check" id="id_form-0-print_report_check"><input type="hidden" name="form-0-PAN_ID" value="10012000" disabled id="id_form-0-PAN_ID"></td></tr>
 [{'PAN_ID': '10012000', 'print_report_check': True}]






class ListFormSetView(ListView):
    context_formset_name = "formset"


    def get_factory_object(self):

        return modelformset_factory(
            self.model,
            form=self.form_class,
            extra=0,
        )
        
    def get_context_data(self, *, object_list=None, **kwargs):

        context_formset_name = getattr(
            self, "context_formset_name")

        context_data = super().get_context_data(**kwargs)
        

        object_list = context_data["object_list"]

        initial_data = self.get_initial(object_list)
        context_data[context_formset_name] = self.get_factory_object()(
            queryset=object_list, initial=initial_data
        )
        
        print(self.request.method , "\n", context_data[context_formset_name], "\n" ,initial_data )

        return context_data

    def get_initial(self, queryset=None):
        """Generate the initial data for the paginated queryset for the current page"""

        if queryset is None:
            queryset = []

        try:
            return self.initial
        except AttributeError:

            data = [
                {**{
                    field_name: getattr(item, field_name)
                    for field_name in self.form_class._meta.fields
                    if hasattr(item, field_name)
                }, "print_report_check":True}
                for item in queryset
            ]

            if self.request.method == "GET":
                try:
                    userids_for_checked_boxes = PrintStatementData["to_download"][
                        PrintStatementData["list-formset-view_previous-page"]
                    ]
                except KeyError:
                    pass
                else:
                    for i in data:
                        if i["PAN_ID"] in userids_for_checked_boxes:
                            i["print_report_check"] = True

            return data

    def get_formset_for_previous_page(self):
        """Set the `page_kwarg` 's  value to the previous page's value because
        the formset's data needs to be compared to the previous page's data.
        The `paginate_queryset` method uses self.kwargs["page_kwarg"]
        to get the queryset corresponding to a page.So just to get the
        initial data corresponding to the previous page through `self.get_form_kwargs` we first need to do this."""

        PrintStatementData[
            "list-formset-view_current-page"] = int(self.request.GET.get(
                self.page_kwarg))

        self.kwargs[self.page_kwarg] = PrintStatementData.get(
            "list-formset-view_previous-page")

        formset = self.get_factory_object()(**self.get_form_kwargs())

        self.kwargs[self.page_kwarg] = PrintStatementData.get(
            "list-formset-view_current-page")

        return formset


VISHESH MANGLA

unread,
Jun 13, 2021, 2:41:53 PM6/13/21
to Django users
In line 58  you can see me adding  "print_report_check":True,  


my form

class PrintReportModelForm(ModelForm):
    print_report_check = forms.BooleanField(widget=forms.CheckboxInput(), required=False)
    class Meta:
        fields = ["PAN_ID""print_report_check"]
        model = User
        widgets = {"PAN_ID"forms.HiddenInput()}
    
    def __init__(self,*args, **kwargs): 
        super().__init__(*args, **kwargs)   
        self.fields["PAN_ID"].disabled = True
     #   self.fields["print_report_check"].initial = True

    def save(selfcommit=True):
        logger.info(" Method not meant to save anything ")
        pass
Reply all
Reply to author
Forward
0 new messages