RamezIssac/django-slick-reporting

Issue with simple `group_by` view

daweedm opened this issue · 9 comments

Hello, I have the following SlickReportingView which works well :

REPORT_NAME = 'report_sales_order_test'

class EmbeddedSalesOrderReportView(SlickReportView):
    name = f"{REPORT_NAME}_embedded"

    template_name = 'admin/report/slick_reporting/simple_report.html'

    report_model = get_order_model()

    date_field = 'issue_datetime'

    excluded_fields = ['payments']

    columns = [
        'customer__company_name',
        '_total_without_taxes'
    ]

and give me the following results (the _total_without_taxes column is showing values, which is the expected behaviour) :

Screenshot 2022-12-24 at 15 21 05

But, once I add a group_by property like this :

class EmbeddedSalesOrderReportView(SlickReportView):
    name = f"{REPORT_NAME}_embedded"

    template_name = 'admin/report/slick_reporting/simple_report.html'

    report_model = get_order_model()

    date_field = 'issue_datetime'

    group_by = 'customer'  # HERE

    excluded_fields = ['payments']

    columns = [
        'company_name',
        SlickReportField.create(  # AND HERE
            Sum,
            '_total_without_taxes',
            name='total_without_taxes',
            verbose_name=_('Total without taxes')
        )
    ]

The resulting value of the aggregation field _total_without_taxes is always zero (see 2nd screenshot) but it's wrong since, as shown on the 1st screenshot, the model instances stored in the database have values for _total_without_taxes field that are greater than zero.

Screenshot 2022-12-24 at 15 30 22

@RamezIssac Could you help me with that ?

Thanks

Note: the customer FK being used for the group_by in the 2nd example points to the following model (note the custom primary key)

class Customer(models.Model):

    hash = CharField(max_length=64, unique=True, primary_key=True)

    edit_datetime = DateTimeField(default=timezone.now, blank=True)

    company_name = CharField(_('Company name'), max_length=200, null=True, blank=True)

Hello hello ,
Just got this , hmmm i will check .

Hello @RamezIssac
Happy New Year! Did you had some time to check this issue? Thank you

Hello ,
Started checking and saw the issue ... Will get back to you soon !

Ok @daweedm , I believe it's fixed in https://pypi.org/project/django-slick-reporting/0.6.5/
Let me know if all is good.

@RamezIssac Thanks a lot, it works!

I have a last question :

When I use group_by with a FK field (using __), how can I keep the reference to the "grouped by" model ?
See the example with comments below:

class EmbeddedSalesOrderReportView(SlickReportView):
    name = 'report'

    template_name = 'admin/report/slick_reporting/simple_report.html'

    report_model = get_order_model()

    date_field = 'issue_datetime'

    group_by = 'customer__origin'  # HERE, I'm grouping with a field of the customer FK

    columns = [
        'company_name',  # HERE, now the ReportView tries to read this field from the 'report_model' model and not the 'customer__origin' model
        SlickReportField.create(
            Sum,
            '_total_without_taxes',
            name='total_without_taxes',
            verbose_name=_('Total without taxes')
        )
    ]

Note that both customer and customer__origin have a company_name field, but the ReportView tries to read that field on the report_model instead of the group_by model.

Also, I searched in the source code looking for a workaround and I discovered that we can define method field on the generator class, so I tried the following :

class CustomGenerator(ReportGenerator):
    def company_name(self, *args, **kwargs):  # row or obj parameter ?
        return 'test'

class EmbeddedSalesOrderReportView(SlickReportView):
   report_generator_class = CustomGenerator  # HERE
   # ...

But unfortunately, the method is never called.

Again, thanks for your help.

Thank you for the clear report and for enriching the package .

Both issues

  1. not using correct model when traversing in group By
  2. method on the generator is not getting called

Should be fixed here https://github.com/ra-systems/django-slick-reporting/tree/group_by_related_column

Let me know if everything is alright 🥂

Sorry for the late, reply, it works well! Thank you very much