1

I'm trying to access a field in views.py in order to perform an operation on it which will calculate its distance from a user-inputted point (using geopy). The input needs to be a plain string but is currently stored in my database as: u'(lat, lng)' which geopy does not enjoy.

How can I store the information in my db in the proper format ( ex. (lat, lng) ). The coordinates are originally inputted in this format.

Thank you in advance, apologies for not using proper terminology, new to this and self taught.

Matt Parrilla
  • 3,171
  • 6
  • 35
  • 54

2 Answers2

0

Use django-annoying

class MyModel(models.Model):
    ...
    lat_lng = JsonField()

And store:

MyModel.objects.create(lat_lng={'data': (lat, lng)}, ...)

And use:

my_model_object.lat_lng['data']
satels
  • 789
  • 6
  • 13
0

Are you saying that the actual value stored in the table is u'(lat, lng)'? Are you converting your string to another representation before storing it? This doesn't look right.

If necessary, you can change the encoding the table uses. Read about that here: How to set the encoding for the tables' char columns in django?

Also you can encode/decode strings to a specific charset before using them in python: What is the difference between encode/decode?

Community
  • 1
  • 1
Max
  • 6,901
  • 7
  • 46
  • 61
  • I'm not quite sure what you mean "converting your string to antoher representation before storing it" sounds like that might be the case though. Here's what I've got `class Place(models.Model): coordinates = models.CharField(max_length=60) def save(self, *args, **kwargs): self.coordinates = Location(self.zipcode, self.street) super(Venue, self).save(*args, **kwargs)` Then I try and access coordinates in views.py (for use in a method which calculates the distance between 2 sets of coordinates) but get u'(lat, lng) as my output – Matt Parrilla Apr 26 '11 at 21:01