views:

310

answers:

2

I am storing the the latitude and longitude as a charfield like ("latitude, longitude"). I prefer to keep it this way.

I need to filter the results to show only latitude > w, latitude < x, longitude > y, longitude < z.

How can I do this without change how I store the lat,long?

+2  A: 

The Django ORM isn't going to be able to help you filter on that field. Your schema isn't designed properly for the type of query you're attempting.

My suggestions:

  • Modify your schema to use separate numeric columns to begin with. (I know you are specifically looking for an option that avoids this but it's still the proper solution).

  • Add two new numeric columns and have those columns updated/set whenever a change is made to the model. You would also need to do a one-time mass update to set the initial values for all of the existing rows.

  • You may be able a rig up a custom SQL query that will go through each row and split the column to retrieve the separate numeric values for latitude and longitude and filter on them. In this case you're pretty much bypassing the ORM and going directly to SQL by yourself and will need to use whatever split methods are available on your database (custom split function, regex, etc). The query will be slow.

  • Add a method to your model that will split the string in Python and return the numeric data. To do any kind of filtering use this data you will need to pull all of the models from your database (expensive) and do the filtering in memory (fast but you lose the advantages of SQL and/or the ORM). This option probably won't scale very well.

Good luck...

Lance McNearney
A: 

As one of the commenters mentioned, you should definitely look at Geodjango, which offers native geo-information support. From their page: GeoDjango is an add-on for Django that turns it into a world-class geographic web framework. GeoDjango strives to make at as simple as possible to create geographic web applications, like location-based services. Some features include:

  • Django model fields for OGC geometries, that may be edited in the admin.
  • Extensions to Django’s ORM for the querying and manipulation of spatial data.
  • Loosely-coupled, high-level Python interfaces for GIS geometry operations and data formats.
DrDee