django-slick-reporting icon indicating copy to clipboard operation
django-slick-reporting copied to clipboard

How to add a ComputationField that sums up data from a field in a ForeignKey (via Related_name)

Open smseidl opened this issue 8 months ago • 6 comments

I'm making some progress on django-slick-reporting, but have hit a snag. I'm trying to sum up a field that is not in the direct model, but is available via the Foreign Key (using related_name). I've gotten a result, but it doesn't seem to add up correctly. I haven't been able to figure out how to reference the specific field in the Foreign module. Can you provide any details?

Foreign Model:

class PaymentDetail(models.Model):
    """Partial Payments from a payment for each specific visit/payment amount"""
    parentPayment=models.ForeignKey(Payment, on_delete=models.CASCADE, null=False,verbose_name="Payment",related_name="payment_detail")
    visit = models.ForeignKey(Visit, on_delete=models.CASCADE, null=False,related_name="payment_detail",verbose_name="Visit")
    dtlAmount = models.DecimalField(max_digits=9,decimal_places=2,verbose_name="Detail/Sub Payment Amount",)

Report View:

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="Count of Visits",name="sum__value"
        ),
        ComputationField.create(
            Sum, "charge", name="sum__charge", verbose_name="Total Charged $"
        ),
        #Sum up related payment amounts that are linked by the "payment_detail" related_name/Foreignkey
        ComputationField.create(
            Sum, "payment_detail", name="sum__paid", verbose_name="Total Paid $"
        ),        
    ]

Sorry for creating an issue for help, but I haven't found anything else.

smseidl avatar Jun 12 '24 16:06 smseidl