See:  
https://stackoverflow.com/questions/54412377/adding-values-to-django-queryset 

VIEW

def position_view(request):

    packet_data = TncData.objects.exclude(lat=0).order_by('-dt_heard')[:100]
    for packet in packet_data:
        *# my complex math calculation*
*        packet.distance = "# complex math formula using packet.lat and 
packet.lon"*

    return render(
        request, 
        'main/position.html',
        {'packet_data': packet_data, 'distance': distance})

TEMPLATE

*# suggest you use a better term than "field" => maybe "record"?*
{% for field in packet_data %}
    <tr class='atable'>
    <td class='atable'>{{field.callsign}}</td>
    <td class='atable'>{{field.lat|floatformat:2}} N 
{{field.lon|floatformat:2}} W </td>
    <td class='atable'></td>
    *<td class='atable'>{{field.distance}}</td>*


HTH!

On Tuesday, 20 July 2021 at 22:25:04 UTC+2 mab.mo...@gmail.com wrote:

> Hello,
>
> I am trying to perform a complex math calculation from a django query set. 
> How can I pass the results to the template for display? Annotations won't 
> work since the math is not a simple sum, diff or average. 
>
> MODEL
>
> class TncData(models.Model):
>     callsign = models.TextField(max_length=20)
>     lat = models.FloatField(null=True, blank=True, default=None)
>     lon = models.FloatField(null=True, blank=True, default=None)
>     path = models.TextField(max_length=250)
>     message = models.TextField(max_length=250)
>     dt_heard = models.DateTimeField(auto_now_add=False)
>
>     def __str__(self):
>         return str(self.callsign)
>
> VIEW
>
> def position_view(request):
>
>     packet_data = 
> TncData.objects.exclude(lat=0).order_by('-dt_heard')[:100]
>
>     *# my complex math calculation*
> *     # need to do something like append distance to packet_data like 
> packet_data.distance*
>
> *    distance = "# complex math formula using packet_data.lat and 
> packet_data.lon"*
>
>     return render(request, 'main/position.html', 
> {'packet_data':packet_data,})
>
> TEMPLATE
>
> {% for field in packet_data %}
>
>     <tr class='atable'>
>     <td class='atable'>{{field.callsign}}</td>
>     <td class='atable'>{{field.lat|floatformat:2}} N 
> {{field.lon|floatformat:2}} W </td>
>     <td class='atable'></td>
>     *<td class='atable'>{{packet_data.distance}}</td>*
>     <td class='atable'>{{field.dt_heard|date:"D m/d H:i"}}</td>
>     </tr>
>
> {% endfor %}
>                          
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/dfe26aa8-1947-4576-9700-2fec733894e7n%40googlegroups.com.

Reply via email to