Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geoposition Field save manually #78

Open
Alejoss opened this issue Oct 26, 2016 · 5 comments
Open

Geoposition Field save manually #78

Alejoss opened this issue Oct 26, 2016 · 5 comments

Comments

@Alejoss
Copy link

Alejoss commented Oct 26, 2016

I need to save a Geoposition Field manually. How can I do that?

class Address(models.Model):
    position = GeopositionField(null=True, db_index=True)

>>>  x.position = {'lat': 52.5, 'lng': 13.4}
>>>  x.save()
>>> x.position.latitude
Traceback (most recent call last):
  File "<console>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'latitude'
>>> x.id
1
y = Address.objects.get(id=1)
InvalidOperation: Invalid literal for Decimal: u"{'lat': 52.5"

If you believe this is a bug, as I suspect. I could solve it and collaborate with the library.

@Alejoss
Copy link
Author

Alejoss commented Oct 26, 2016

I took the {'lat': 52.5, 'lng': 13.4} from the docs.

... I believe the bug is in the GeopositionField, in the to_python() function:

def to_python(self, value):
    if not value or value == 'None':
        return None
    if isinstance(value, Geoposition):
        return value
    if isinstance(value, list):
        return Geoposition(value[0], value[1])

    # ! It could be a dict (if the user sets the values manually like this: {'lat': 52.5, 'lng': 13.4})

    # default case is string  <--- This is not correct. 
    value_parts = value.rsplit(',')
    try:
        latitude = value_parts[0]
    except IndexError:
        latitude = '0.0'
    try:
        longitude = value_parts[1]
    except IndexError:
        longitude = '0.0'

@MaxLarue
Copy link

MaxLarue commented Apr 7, 2017

Hello,

After applicating your change to my version of geoposition i was still left with one trouble which was that lat and long would always be 0.

This is due to the fact value_parts.rsplit('s') ended up in ["[Decimal('50.8330962')", " Decimal('4.358464599999934')]"], making the conversion raise an exception. Changing the code to remove litterals from the string made everything go fine. wonder if it helps. But thanks anyway

@ghost
Copy link

ghost commented Aug 24, 2017

Changing

....
        value_parts = value.rsplit(',')
....

to

....
       value_parts = eval(value)
       if isinstance(value_parts,dict):
            value_parts=(value_parts['lat'],value_parts['lng'])
....

worked nicely for me.

@elcolie
Copy link

elcolie commented Nov 6, 2017

Hi. I am unable to save the instace. Could you please explain a bit in detail then I can workaround on this issue.

@elcolie
Copy link

elcolie commented Nov 6, 2017

Okay. Just for now. This is my workaround on the https://github.com/philippbosch/django-geoposition/archive/django-1.11.zip

models.py

class AbstractAddress(models.Model):
    ...
    position = GeopositionField()
comp.position = "23.2,122.0"
comp.save()

The position has to be string concatenate with string.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants