开发者

How can I store a non-unicode string in the database of my Django App?

I'm trying to access a field in v开发者_运维百科iews.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.


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']


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?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜