1

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.

lotr34
  • 13
  • 1
  • 3

1 Answers1

1

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)
AKX
  • 152,115
  • 15
  • 115
  • 172
  • Greatful !! should i make any change at settings.py ? in installed apps section or somewhere else ? – lotr34 Jun 07 '21 at 09:57
  • Might want to use Flask rather than Django then. – Toni Sredanović Jun 07 '21 at 10:00
  • Unfortunately my company using influxdb for a while. This because why i cant change the db – lotr34 Jun 07 '21 at 10:09
  • No, this has nothing to do with `settings.py`, or even Django really. Of course you can put those connection settings in your `settings.py` if you like. – AKX Jun 07 '21 at 11:02
  • @ToniSredanović That's not really good advice if OP wants e.g. Django's authentication and all the other included batteries. – AKX Jun 07 '21 at 11:02
  • @AKX When i tried to import InfluxDBClient its throw an error as Unresolved reference 'InfluxDBClient' . How can i fixed it ? – lotr34 Jun 07 '21 at 11:45
  • Did you install the package? Make sure you don't have other files called `influxdb_client.py`. – AKX Jun 07 '21 at 11:46