RamezIssac / django-slick-reporting

The Reporting Engine for Django. Create dashboards and standalone Reports and Charts.
https://django-slick-reporting.com/
BSD 3-Clause "New" or "Revised" License
452 stars 39 forks source link

Issue using excluded_fields #110

Open smseidl opened 1 week ago

smseidl commented 1 week ago

Hi again - sorry for creating so many issues :-( This one seems to be an actual issue. I'm trying to use the excluded_fields in my report View to remove some options from the filter form. My model has a bunch of different Foreign Keys that are showing up in the form. I want to limit that some what. When I put documents in the excluded_fields list it works, but I am unable to add any of the other without producing an error.

class Visit(models.Model):
    """Details for a medical visit"""
    date = models.DateField(null=False)
    patient = models.ForeignKey(Member, on_delete=models.PROTECT)
    provider = models.ForeignKey(Provider, on_delete=models.PROTECT)
    insurance = models.ForeignKey(InsurancePlan, on_delete=models.SET_NULL,blank=True, null=True)
    service = models.ForeignKey(Service,on_delete=models.PROTECT,verbose_name="Service",blank=False,null=False,default=Service.get_default_pk)
    eob = models.ForeignKey(EOB, on_delete=models.SET_NULL,null=True,blank=True,)
    documents = models.ManyToManyField(Document, related_name="Visits", )  

Error

2024-06-19 19:29:57 - ERROR - django.request - log_response - Internal Server Error: /reports/
Traceback (most recent call last):
  File "/workspaces/medicalexpensetracker/.venv/lib/python3.10/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
  File "/workspaces/medicalexpensetracker/.venv/lib/python3.10/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/workspaces/medicalexpensetracker/.venv/lib/python3.10/site-packages/django/views/generic/base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
  File "/workspaces/medicalexpensetracker/.venv/lib/python3.10/site-packages/django/contrib/auth/mixins.py", line 135, in dispatch
    return super().dispatch(request, *args, **kwargs)
  File "/workspaces/medicalexpensetracker/.venv/lib/python3.10/site-packages/django/views/generic/base.py", line 143, in dispatch
    return handler(request, *args, **kwargs)
  File "/workspaces/medicalexpensetracker/.venv/lib/python3.10/site-packages/slick_reporting/views.py", line 185, in get
    form_class = self.get_form_class()
  File "/workspaces/medicalexpensetracker/.venv/lib/python3.10/site-packages/slick_reporting/views.py", line 248, in get_form_class
    return self.form_class or report_form_factory(
  File "/workspaces/medicalexpensetracker/.venv/lib/python3.10/site-packages/slick_reporting/forms.py", line 271, in report_form_factory
    del fkeys_map[excluded]
KeyError: 'eob'
RamezIssac commented 1 week ago

Can you share your ReportView ?

smseidl commented 1 week ago

Sorry - busy weekend. Here's the View you wanted and a sample of one of the Class based ComputationField... all are very similar in definition.

@report_field_register
class TotalCCPaymentField(ComputationField):
    name = "sum__paidCC"
    calculation_field = "v_pay_dtl__paydetail_amount"  # the field we want to compute on
    calculation_method = Sum  # What method we want, default to Sum
    verbose_name = "Total CC Paid $"
    # base_q_filters={"v_pay_dtl__parentPayment__type","CC"}

    def get_queryset(self):
        queryset = self.report_model.objects
        # if self.base_q_filters:
        queryset = queryset.filter(v_pay_dtl__parentPayment__type="CC")
        # if self.base_kwargs_filters:
            # queryset = queryset.filter(**self.base_kwargs_filters)
        return queryset.order_by()

class ReportPageView(ReportView):
    report_title = "Visits by Patient"
    report_model = Visit
    date_field = "date"
    group_by = "patient"
    excluded_fields =["documents"]

    columns = [
        "name",
        ComputationField.create(
            Count, "id",verbose_name="# Visits",name="sum__value"
        ),
        isPaidField,
        ComputationField.create(
            Sum, "charge", name="sum__charge", verbose_name="Total Charged $"
        ),
        TotalPaidReportField,       #similar to TotalCCPaymentField
        TotalCCPaymentField,    
        TotalCheckPaymentField, #similar to TotalCCPaymentField
        TotalEXPPaymentField,     #similar to TotalCCPaymentField
    ]

    chart_settings = [
        Chart(
            "Total Visits",
            Chart.BAR,
            data_source=["sum__value"],
            title_source=["name"],
        ),
        Chart(
            "Total Charges $ [PIE]",
            Chart.PIE,
            data_source=["sum__charge"],
            title_source=["name"],
        ),
        Chart(
            "Total Unpaid $ [PIE]",
            Chart.PIE,
            data_source=["count__ispaid"],
            title_source=["name"],
        )        
    ]