Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model's Meta ordering field added when using group_by #10

Closed
squio opened this issue Nov 25, 2020 · 1 comment
Closed

Model's Meta ordering field added when using group_by #10

squio opened this issue Nov 25, 2020 · 1 comment

Comments

@squio
Copy link
Contributor

squio commented Nov 25, 2020

Use case: simple report for model grouped by one to many foreign key related model as in the Group By Report example.

Code example; Groups have many Sessions through a ForeignKey relation in Session.

Session:

class Session(model.Model):

    group = models.ForeignKey(
        'my_app.Group',
        blank=True, null=True,
        on_delete=models.PROTECT)
    start_time = models.FloatField(
        null=True, verbose_name=_('Start time'), db_index=True)
    created_at = models.DateTimeField(
        null=True, verbose_name=_('Created at'))

    # ...
    class Meta:
        db_table = 'myapp_session'
        ordering = ['-start_time']

In my reports configuration:

class TotalSessions(SlickReportView):
    """ Sessions per group """

    report_model = Session
    date_field = 'created_at'
    group_by = 'group'
    columns = [
        'name',
        SlickReportField.create(Sum, 'distance', name='sum__distance'),
        SlickReportField.create(Sum, 'duration', name='sum__duration'),
    ]

Now my reports don't show the correct aggregate data but rather just the values for one group session.

The generated SQL from the debugger shows why: the field start_time from Session._meta['ordering'] is added to the group_by clause:

SELECT `myapp_session`.`group_id`,
       SUM(`myapp_session`.`distance`) AS `distance__sum`
  FROM `myapp_session`
 WHERE (`myapp_session`.`created_at` > '2019-12-31 23:00:00' AND `myapp_session`.`created_at` <= '2020-12-31 23:00:00')
 GROUP BY `myapp_session`.`group_id`,
       `myapp_session`.`start_time`

Removing the field ordering in Session class Meta removes the extra group_by field from the SQL query and gives the correct results.

I'm not sure where to look for this issue, the word 'ordering' does not occur in the django-slick-reporting source anywhere!?

RamezIssac added a commit that referenced this issue Nov 25, 2020
Remove default order by while generating the report #10
@RamezIssac
Copy link
Owner

Thank you for the comprehensive issue report !
Issue addressed on develop branch

I'm not sure where to look for this issue, the word 'ordering' does not occur in the django-slick-reporting source anywhere!?
Yeah order by is not mentioned, now it is mentioned to disable the initial order_by that might be on a model :)

@squio squio closed this as completed Nov 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants