Search code examples
djangoinfluxdbinfluxdb-python

How can i use influxdb in django project


i have some trouble about influxdb+django configurations.

Firstly let me summarize my situation. I have an influxdb which is already collecting data from endnode(sensors). Data is transfering by LoraWan technology. I can read that datas from terminal by writing flux queries so database is working without any problem.

Now my second phase of this project is visualizing that datas on an web page. I am using django framework for that i completed the frontend parts nearly. I looked on internet for the configurations for influxdb on django but i couldnt handle it. In django documentation page they are listed some databases like below:

Django officially supports the following databases:

PostgreSQL MariaDB MySQL Oracle SQLite

How will i use/configure and get data from my influxdb ? Is it possible ? What are the alternative solutions.


Solution

  • Sure, Django doesn't support InfluxDB for its usual models (authentication and what-have-you, and of course your own apps), but you can simply use the InfluxDB Python client library to make a query in a view and e.g. return JSON data.

    Adapting from the readme, you might have a view like

    from influxdb_client import InfluxDBClient
    
    client = InfluxDBClient(url="http://localhost:8086", token="my-token", org="my-org")
    
    def get_data(request):
        bucket = "my-bucket"
        query_api = client.query_api()
        result = query_api.query_csv('from(bucket:"my-bucket") |> range(start: -10m)')
        return JSONResponse(result)