Using south... it would take 3 migrations to address this. (Link: How to install South]
1) Add a PointField
called point
:
from django.contrib.gis.db import models <-- NEW
# ...
class Location(models.Model):
#other fields here
lat = models.CharField(blank=True, max_length=100)
lng = models.CharField(blank=True, max_length=100)
point = models.PointField(null=True) <-- NEW
2) Perform a schemamigration
automagically
patrick@localhost:~$ python manage.py schemamigration my_app_name --auto
3) Perform a custom datamigration
where you will create a point on each element from the existing CharField
attributes
patrick@localhost:~$ python manage.py datamigration my_app_name latlng_to_point
Edit the file that is created by the datamigration
management command:
# my_app_name/migrations/0034__latlng_to_point.py
from django.contrib.gis.geos import Point
# ...
class Migration(DataMigration):
def forwards(self, orm):
"Write your forwards methods here."
# Note: Remember to use orm['appname.ModelName'] rather than "from appname.models..."
for location in orm['my_app_name.Location'].objects.all():
location.point = Point(float(location.lng), float(location.lat), srid=4326)
location.save()
def backwards(self, orm):
"Write your backwards methods here."
for location in orm['data.Location'].objects.all():
location.point = None
location.save()
4) Delete the CharField
from your model and remove null=True
if you wish:
class Location(models.Model):
# other fields here
# DELETED lat = models.CharField(blank=True, max_length=100)
# DELETED lng = models.CharField(blank=True, max_length=100)
point = models.PointField(null=False) <-- NEW: null=False
5) Create another schemamigration automatically
patrick@localhost:~$ python manage.py schemamigration my_app_name --auto
6) Run the migrations successfully! This is when changes are written to your DB, etc.
patrick@localhost:~$ python manage.py migrate my_app_name