title: How to calculate 3D distance (including altitude) between two points in GeoDjango
body: <p><strong>Prologue:</strong></p>
  
  <p>This is a question arising often in SO:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/43474570/3d-distance-calculations-with-geodjango/43807804#43807804">3d distance calculations with GeoDjango</a></li>
  <li><a href="https://stackoverflow.com/questions/33111529/calculating-distance-between-two-points-using-latitude-longitude-and-altitude-e/42394979#42394979">Calculating distance between two points using latitude longitude and altitude (elevation)</a></li>
  <li><a href="https://stackoverflow.com/questions/40024322/distance-between-two-3d-point-in-geodjango-postgis/42644003#42644003">Distance between two 3D point in geodjango (postgis)</a></li>
  </ul>
  
  <p>I wanted to compose an example on SO Documentation but the <code>geodjango</code> chapter never took off and since the Documentation got shut down on August 8, 2017, I will follow the suggestion of <a href="https://meta.stackoverflow.com/questions/354217/sunsetting-documentation/354302#354302">this widely upvoted and discussed meta answer</a> and write my example as a self-answered post.</p>
  
  <p>Of course, I would be more than happy to see any different approach as well!!</p>
  
  <hr>
  
  <p><strong>Question:</strong></p>
  
  <p>Assume the model:</p>
  
  <pre><code>class MyModel(models.Model):
      name = models.CharField()
      coordinates = models.PointField()
  </code></pre>
  
  <p>Where I store the point in the <code>coordinate</code> variable as a <code>lan, lng, alt</code> point:</p>
  
  <pre><code>MyModel.objects.create(
      name='point_name', 
      coordinates='SRID=3857;POINT Z (100.00 10.00 150)')
  </code></pre>
  
  <p>I am trying to calculate the 3D distance between two such points:</p>
  
  <pre><code>p1 = MyModel.objects.get(name='point_1').coordinates
  p2 = MyModel.objects.get(name='point_2').coordinates
  
  d = Distance(m=p1.distance(p2))
  </code></pre>
  
  <p>Now <code>d=X</code> in meters.</p>
  
  <p>If I change only the altitude of one of the points in question:</p>
  
  <p>For example:</p>
  
  <pre><code>p1.coordinates = 'SRID=3857;POINT Z (100.00 10.00 200)'
  </code></pre>
  
  <p>from 150 previously, the calculation:</p>
  
  <pre><code>d = Distance(m=p1.distance(p2))
  </code></pre>
  
  <p>returns <code>d=X</code> again, like the elevation is ignored.<br>
  How can I calculate the 3D distance between my points?</p>