Value not being displayed

Hi,

I have a function that runs some calculations and presents the values in a table.

For one of the sums it never shows the values in the calculation for anything below 0. I’ve tested the calculations manually and the sum returns 0.02 but in the table, it’s 0

        avg_interactions_per_impression=Case(
        When(impressions=0, then=Value(0)),
        default=F('interactions') / F('impressions') ,
        output_field=FloatField()
        ),

There are no filters on the table in the html, so I can’t work out why this is happening.

the same calc that returns bigger values they get presented in the table, so i think its something to do with the 0.0

I think we may need more context than this.

What is the model being queried?

What’s the complete query? (Is this an annotation, aggregation, or subquery?)

What specifically do you mean by:

What does the view and the portion of the template rendering this value look like?

Hi, Ken.

The view is:

    project_data = ProjectMetric.objects.filter(capture_date=current_date, project__name=OuterRef('project__name')).order_by('-capture_date').values('followers', 'interactions')[:1]
    weekly_sums = ProjectMetric.objects.filter(
        capture_date=current_date,followers__gt=0
        ).values('project__name','impressions','interactions').annotate(
        avg_impressions_per_day=Case(
        When(impressions=0, then=Value(0)),
        default=F('impressions') / 7,
        output_field=FloatField()
        ),
        avg_interactions_per_impression=Case(
        When(impressions=0, then=Value(0.1)),
        default=F('interactions') / F('impressions') ,
        output_field=FloatField()
        ),
        avg_post_per_day=Case(
        When(posts=0, then=Value(0)),
        default=F('posts') / 7,
        output_field=FloatField()
        ),
        ...
    )
    
    return weekly_sums
{% for result in metric_results  %}                         
  
<tr>
  <td class="table-text">{{ result.project__name}}</td>
  <td class="table-text table-row-text ">{{ result.followers | intcomma }}</td>
  <td class="table-text">{{ result.avg_impressions_per_day|intcomma}}</td>
  <td class="table-text">{{ result.avg_interactions_per_impression}}</td>
...

I call the function outside of the view which renders the template

metric_results = metrics()

There are about 100 projects, for about 90 of them this avg_interactions_per_impression displays the values in the table, but they are all like 2.0, or 5.1 etc. But for the remaining 10 where the values are 0.x they dont show?

If you run your query in the Django shell, what you you get for avg_interactions_per_inpression for those 10 projects?

Is it only that one field that is causing problems here?

i get 0.0, but i have checked the values for default=F('interactions') / F('impressions') in the database for the current date and manually done the calcs and the value isn’t 0. For some, it is cause impressions is 0 but for others there are values.

Just as a real wild guess, you might want to try casting those values to be float fields in the division expression. I’m guessing there’s the chance that it’s doing the integer division before converting the output to a FloatField.

That’s a good idea. I’ll give that a go and come back.

Thanks, Ken.

Tom

You got it, Ken :slight_smile:

        avg_interactions_per_impression=Case(
        When(impressions=0, then=Value(0)),
        default=Cast(F('interactions'), FloatField()) / Cast(F('impressions'), FloatField()),
        output_field=FloatField()
        ),

Thanks again for your help.

Tom.