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
ValidationError ['Invalid input for a Location instance. Latitude must be convertible to float '] #15
Comments
Hello, what value is saved in the database? |
It will be beacuse the value (as string) is converte to string accesing like a list ("{},{}".format(value[0], value[1])) and save to the database. For example if I add it to a migrate with default value. migrations.AddField(
model_name='order',
name='destination_location',
field=mapbox_location_field.models.LocationField(default='0,0', map_attrs={}),
preserve_default=False,
) All updated fields will have '0,' as value in the database. If I implement for example django-rest-framework with LocationField I have the same result. I solved temporaly adding "or isinstance(value, str)" to the LocationField model. def get_prep_value(self, value):
if value is None or isinstance(value, str):
return value
return "{},{}".format(value[0], value[1]) |
Yeah, this error probably occurs when the value from db is converted to Python representation. However, I raise it purposely when parsing is impossible. It means you have some wrong value stored in db. |
django-rest-framework take the field like charfield and if you send it like a string it save only de two first chars. For example if I send "12.23,11.40" save like "1,2" in the database. |
I haven't used my package with DRF, but something seems definitely wrong, so I will take a closer look at it today. |
Ok, so basically I changed get_prop_value function because it used to assume that you always provide a tuple or list and not a string value. Of course, DRF's serialization with CharField is a clear example, why it was a wrong assumption. Since 1.6.1 it's fixed. However, saving string values is not a safe procedure and can potentially cause multiple errors (similar to @sajib1066 ). That's why I implemented the protection mechanism and when string can't be parsed, the default value of null (in case of nullable field) or "0,0" (in case of non-nullable field) is saved to db. If it's undesired behavior, then you have to implement a validation mechanism in your API. It's quite simple. That's a code example: from rest_framework import serializers
from mapbox_location_field.forms import parse_location, ValidationError
class LocationSerializer(serializers.HyperlinkedModelSerializer):
class Meta:
model = SomeLocationModel
fields = ["location"]
def validate_location(self, value):
try:
parse_location(value)
except ValidationError as error:
raise serializers.ValidationError(error.message)
return value It's serializer for |
I hope I resolved this issue. If you still encounter it, reopen it, or create a new one. If you have some wrong values stored in db, you have to clean it. |
when I goto my location list after create location then found this error.
please solve it urgently.
The text was updated successfully, but these errors were encountered: