Thank you Derek for the information. For those that are having similar 
problems this is the solution that worked for me based on Derek's lead....

## METHOD 1 ##

# view

def position_view(request):

    packet_data = TncData.objects.exclude(lat=0).order_by('-dt_heard')[:100]

    # loop through query set and add new dictionary key-value pair

    for row in packet_data:
        
        row.distance = row.lat*row.lon # example only of calculation  only 
not real 

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

# html template snippet

{% for row in packet_data %}

    <tr class='atable'>
    <td class='atable'>{{row.callsign}}</td>
    <td class='atable'>{{row.lat|floatformat:2}} N 
{{row.lon|floatformat:2}} W </td>
    <td class='atable' 
style='text-align:right;'>{{row.distance|floatformat:1}} miles</td>
    <td class='atable' 
style='text-align:right;'>{{row.bearing|floatformat:0}} deg</td>
    <td class='atable' style='padding-left:20px;'>{{row.dt_heard|date:"D 
m/d H:i"}}</td>
    </tr>

{% endfor %}

## METHOD 2 use @property in the model.py file 

# Put the data calculations in the model.py file

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)

    ## HAVERSINE FORMULA

    @property
    def calculate_distance(self):

        # Variables

        r = 6371 # radius earth km

        lat_1 = radians(42.97)
        lon_1 = radians(89.77)

        lat_2 = radians(self.lat)
        lon_2 = radians(self.lon)

        dlat = lat_1 - lat_2
        dlon = lon_1 - lon_2

        a = sin(dlat / 2)**2 + cos(lat_1) * cos(lat_2) * sin(dlon / 2)**2

        c = 2 * asin(sqrt(a))

        distance_km = c*r
        distance = distance_km * 0.621

        return(distance)

# html template snippet

{% for row in packet_data %}

    <tr class='atable'>
    <td class='atable'>{{row.callsign}}</td>
    <td class='atable'>{{row.lat|floatformat:2}} N 
{{row.lon|floatformat:2}} W </td>
    <td class='atable' 
style='text-align:right;'>{{*row.calculate_distance|*floatformat:1}} 
miles</td>
    <td class='atable' 
style='text-align:right;'>{{row.bearing|floatformat:0}} deg</td>
    <td class='atable' style='padding-left:20px;'>{{row.dt_heard|date:"D 
m/d H:i"}}</td>
    </tr>


{% endfor %}


On Wednesday, July 21, 2021 at 5:01:02 PM UTC-5 mab.mo...@gmail.com wrote:

> Thanks Derek. I'll give it a try
>
> On Wednesday, July 21, 2021 at 8:55:08 AM UTC-5 Derek wrote:
>
>> 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/b5115c63-e000-48ee-8e5d-7f923c12731bn%40googlegroups.com.

Reply via email to