Search code examples
pythonrestpython-requestsweb2py

Posting a list to a web2py database


I have a simple web2py app that implements a RESTful API. In my model, I have something like:

db.define_table('vehicles',
                Field('flightId', type='string', default='00000', length=128, 
                .
                .
                .
                Field('route', 'reference path'),
                )

db.define_table('path',
                Field('coordinates', type='list:integer', requires=IS_NOT_EMPTY()))

I want my vehicle to be able to reference a path, which is then managed by another database entry. This path, of course, will be given by coordinate pairs in latitude/longitude so actually a list of tuples would be best. Is there a more sensible way to do this? How would I post a list of ints? Could I just use requests to do this?

The client needs to be able to query the vehicle a lot, but I would like not to send the full path each time this happens as there are many points along the path. I'm totally out of my element with web stuff, so I appreciate any help, tips or resources anyone can provide in this regard.


Solution

  • I don't see how you would store a sequence of latitude/longitude pairs in a list:integer field. Instead, you might consider the following options:

    1. Make db.path.coordinates a json field, and submit the coordinates as a JSON object. When retrieving records from the table, the web2py DAL will automatically convert the JSON to a Python object for manipulation with Python.
    2. Use a string or text field and create your own custom serialization of the data into a string format, parsing the data yourself when doing inserts and selects.
    3. Implement option #2 using either filter_in/filter_out or a custom field type.