diff --git a/demo_proj/demo_app/helpers.py b/demo_proj/demo_app/helpers.py
index 09e029e..d8271e5 100644
--- a/demo_proj/demo_app/helpers.py
+++ b/demo_proj/demo_app/helpers.py
@@ -43,6 +43,7 @@
("chartjs-examples", reports.ChartJSExample),
("apexcharts-examples", reports.ProductSalesApexChart),
("custom-export", reports.CustomExportReport),
+ ("form-initial", reports.ReportWithFormInitial),
]
diff --git a/demo_proj/demo_app/reports.py b/demo_proj/demo_app/reports.py
index 17aa14d..a7b50e5 100644
--- a/demo_proj/demo_app/reports.py
+++ b/demo_proj/demo_app/reports.py
@@ -672,3 +672,28 @@ def export_csv(self, report_data):
export_csv.title = _("My Custom CSV export Title")
export_csv.css_class = "btn btn-primary"
+
+
+class ReportWithFormInitial(ReportView):
+ report_title = _("Report With Form Initial")
+ report_model = SalesTransaction
+ date_field = "date"
+ group_by = "product"
+
+ columns = [
+ "name",
+ ComputationField.create(
+ method=Sum,
+ field="value",
+ name="value__sum",
+ verbose_name="Total sold $",
+ is_summable=True,
+ ),
+ ]
+
+ def get_initial(self):
+ from .models import Client
+
+ initial = super().get_initial()
+ initial["client_id"] = [Client.objects.first().pk, Client.objects.last().pk]
+ return initial
diff --git a/demo_proj/templates/menu.html b/demo_proj/templates/menu.html
index 5fd97a7..f961660 100644
--- a/demo_proj/templates/menu.html
+++ b/demo_proj/templates/menu.html
@@ -221,4 +221,18 @@
+
+
+
+
+
+
+ Form initial
+
+
+
\ No newline at end of file
diff --git a/slick_reporting/forms.py b/slick_reporting/forms.py
index 07e0544..b81e6f8 100644
--- a/slick_reporting/forms.py
+++ b/slick_reporting/forms.py
@@ -275,14 +275,14 @@ def report_form_factory(
fields["start_date"] = forms.DateTimeField(
required=False,
label=_("From date"),
- initial=initial.get("start_date", app_settings.SLICK_REPORTING_DEFAULT_START_DATE),
+ initial=initial.get("start_date", "") or app_settings.SLICK_REPORTING_SETTINGS["DEFAULT_START_DATE_TIME"],
widget=forms.DateTimeInput(attrs={"autocomplete": "off"}),
)
fields["end_date"] = forms.DateTimeField(
required=False,
label=_("To date"),
- initial=initial.get("end_date", app_settings.SLICK_REPORTING_DEFAULT_END_DATE),
+ initial=initial.get("end_date", "") or app_settings.SLICK_REPORTING_SETTINGS["DEFAULT_END_DATE_TIME"],
widget=forms.DateTimeInput(attrs={"autocomplete": "off"}),
)
@@ -303,6 +303,7 @@ def report_form_factory(
field_attrs = foreign_key_widget_func(f_field)
if name in required:
field_attrs["required"] = True
+ field_attrs["initial"] = initial.get(name, "")
fields[name] = f_field.formfield(**field_attrs)
if crosstab_model:
diff --git a/slick_reporting/generator.py b/slick_reporting/generator.py
index 7f3395a..8c9aafb 100644
--- a/slick_reporting/generator.py
+++ b/slick_reporting/generator.py
@@ -932,7 +932,8 @@ def get_full_response(
}
return data
- def get_chart_settings(self, chart_settings=None, default_chart_title=None, chart_engine=None):
+ @staticmethod
+ def get_chart_settings(chart_settings=None, default_chart_title=None, chart_engine=None):
"""
Ensure the sane settings are passed to the front end.
"""
@@ -943,7 +944,6 @@ def get_chart_settings(self, chart_settings=None, default_chart_title=None, char
for i, chart in enumerate(chart_settings):
if type(chart) is Chart:
chart = chart.to_dict()
-
chart["id"] = chart.get("id", f"{i}")
chart_type = chart.get("type", "line")
if chart_type == "column" and SLICK_REPORTING_DEFAULT_CHARTS_ENGINE == "chartsjs":
diff --git a/slick_reporting/views.py b/slick_reporting/views.py
index c62c23c..92ecbb2 100644
--- a/slick_reporting/views.py
+++ b/slick_reporting/views.py
@@ -275,12 +275,12 @@ def get_form_kwargs(self):
}
)
elif self.request.method in ("GET", "PUT"):
- # elif self.request.GET:
- kwargs.update(
- {
- "data": self.request.GET,
- }
- )
+ if self.request.GET or self.request.headers.get("x-requested-with") == "XMLHttpRequest":
+ kwargs.update(
+ {
+ "data": self.request.GET,
+ }
+ )
return kwargs
def get_crosstab_ids(self):
@@ -392,8 +392,11 @@ def get_chart_settings(self, generator=None):
"""
Ensure the sane settings are passed to the front end.
"""
- generator = generator or self.get_report_generator()
- return generator.get_chart_settings(self.chart_settings or [], self.report_title, self.chart_engine)
+ return self.report_generator_class.get_chart_settings(
+ chart_settings=self.chart_settings or [],
+ default_chart_title=self.report_title,
+ chart_engine=self.chart_engine,
+ )
@classmethod
def get_queryset(cls):
@@ -416,10 +419,14 @@ def get_report_slug(cls):
return cls.report_slug or cls.__name__.lower()
def get_initial(self):
- return {
- "start_date": SLICK_REPORTING_SETTINGS["DEFAULT_START_DATE_TIME"],
- "end_date": SLICK_REPORTING_SETTINGS["DEFAULT_END_DATE_TIME"],
- }
+ initial = self.initial.copy()
+ initial.update(
+ {
+ "start_date": SLICK_REPORTING_SETTINGS["DEFAULT_START_DATE_TIME"],
+ "end_date": SLICK_REPORTING_SETTINGS["DEFAULT_END_DATE_TIME"],
+ }
+ )
+ return initial
def get_form_crispy_helper(self):
"""
@@ -438,8 +445,7 @@ def get_context_data(self, **kwargs):
context["report"] = self
if not (self.request.POST or self.request.GET):
- # initialize empty form with initials if the no data is in the get or the post
- context["form"] = self.get_form_class()()
+ context["form"] = self.get_form_class()(**self.get_form_kwargs())
return context
def form_invalid(self, form):