Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8185135
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T01:44:23+00:00 2026-06-07T01:44:23+00:00

I have a model with PointField for location coordinates. I have a MySQL function

  • 0

I have a model with PointField for location coordinates. I have a MySQL function that calculates the distance between two points called dist. I use extra() “select” to calculate distance for each returned object in the queryset. I also use extra() “where” to filter those objects that are within a specific range. Like this

query = queryset.extra(
    select={
        "distance":"dist(geomfromtext('%s'),geomfromtext('%s'))"%(loc1, loc2)
    },
    where=["1 having `distance` <= %s"%(km)]
) #simplified example

This works fine for getting and reading the results, except when I try counting the resultset I get the error that ‘distance’ is not a field. After exploring a bit further, it seems that count ignores the “select” from extra and just uses “where”. While the full SQL query looks like this:

SELECT (dist(geomfromtext('POINT (-4.6858300000000003 36.5154300000000021)'),geomfromtext('POINT (-4.8858300000000003 36.5154300000000021)'))) AS `distance`, `testmodel`.`id`, `testmodel`.`name`, `testmodel`.`email`, (...) FROM `testmodel` WHERE 1 having `distance` <= 50.0

The count query is much shorter and doesn’t have the dist selection part:

SELECT COUNT( `testmodel`.`id`) FROM `testmodel` WHERE 1 having `distance` <= 50.0

Logically, MySQL gives an error because “distance” is undefined. Is there a way to tell Django it has to include the extra select for the count?

Thanks for any ideas!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T01:44:25+00:00Added an answer on June 7, 2026 at 1:44 am

    You could use a raw query if you are not plannig to use any other database system.

        params = {'point1':wktpoint1, 'point2':wktpoint2}
        query = """
            SELECT
                dist(%(point1)s, %(point2)s)
            FROM
                testmodel
        ;"""
        query_set = self.raw(query, params)
    

    Also, if you need more GIS support, you should evaluate PostgreSQL+PostGIS (If you don’t like to reinvent the wheel, you should not make your own dist function)

    Django offers GIS support through GeoDjango. There you got functions like distance. You should check support here

    In order to use GeoDjango you need to add a field on yout model, to tell them to use the GeoManager, Then you can start doing geoqueries, and you should have no problems with count.

    with mysql you cando something like this using geodjango

    ### models.py
    from django.contrib.gis.db import models
    class YourModel(models.Model):
        your_geo_field=models.PolygonField()
        #your_geo_field=models.PointField()
        #your_geo_field=models.GeometryField()
        objects = models.GeoManager()
    
    ### your code
    from django.contrib.gis.geos import *
    from django.contrib.gis.measure import D
    a_geom=fromstr('POINT(-96.876369 29.905320)', srid=4326)
    distance=5
    YoourModel.objects.filter(your_geo_field__distance_lt=(a_geom, D(m=distance))).count()
    

    you can see better examples here and the reference here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

we have model helper (used by several different models) called set_guids that sets self.theguid
I have model/controller called notification. I want to make a new url so that
I have model with a location, which itself has a latitude and longitude. What
I have model Article it has field title with some text that may contain
In my controller I have model operations that can return empty results. I've setup
I have a situation where I have Model A that has a variety of
I have model that is a queue of Strings associated with enum types. I'm
I have model called test, and test can have many tests, and should be
I have model called Product and it has the following columns: create_table :products do
I have model show which has_many performances and a performance has a certain location.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.