complex math calculation from query set

224 views
Skip to first unread message

mab.mo...@gmail.com

unread,
Jul 20, 2021, 4:25:04 PM7/20/21
to Django users
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 %}
                         

Derek

unread,
Jul 21, 2021, 9:55:08 AM7/21/21
to Django users

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!

Derek

unread,
Jul 21, 2021, 10:05:45 AM7/21/21
to Django users
Ignore the "'distance': distance" in the return - that should not be there.

mab.mo...@gmail.com

unread,
Jul 21, 2021, 6:01:02 PM7/21/21
to Django users
Thanks Derek. I'll give it a try

mab.mo...@gmail.com

unread,
Jul 24, 2021, 10:32:18 AM7/24/21
to Django users
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 %}

esteem learning center

unread,
Jul 24, 2021, 2:36:13 PM7/24/21
to django...@googlegroups.com
If you want to do some basic django calculations, you can use django-mathfilters is simple to use for add, subtract, mul, and div

--
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...@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.

Franck Tchouanga

unread,
Jul 24, 2021, 4:07:02 PM7/24/21
to Track django-users@googlegroups.com
Guys I got a good place to manage all your django and good enough developer and designers for your project just check out the below link to get it all




--
Best Wishes

Mr Tchouanga
Reply all
Reply to author
Forward
0 new messages