简体   繁体   English

在 django 中存储动态数据

[英]Storing dynamic data in django

So, in my application I currently am receiving and storing some data,the structure currently is based around this:因此,在我的应用程序中,我目前正在接收和存储一些数据,目前的结构基于此:

  • Each application has a number of devices每个应用程序都有许多设备
  • Each device has incoming data from its sensors每个设备都有来自其传感器的传入数据
  • Each sensor has a type (eg temperature/humidity/etc)每个传感器都有一个类型(例如温度/湿度/等)

Initially, I had wanted to find a solution to store the data dynamically into one table for all data, with a model that might look like this:最初,我想找到一种解决方案,将所有数据动态存储到一个表中,model 可能如下所示:

time = models.DateTimeField()
sensor = models.ForeignKey(Sensor, on_delete=models.CASCADE)
data = ???

Now, each sensor is connected in the models to all the details (including the application, the sensor hardware information and other API-related information for connecting to said sensors), however, for the data I looked up a number of options but I have yet to find one that feels sufficient, some of the options I explored are:现在,模型中的每个传感器都连接到所有细节(包括应用程序、传感器硬件信息和用于连接到所述传感器的其他 API 相关信息),但是,对于数据,我查找了许多选项,但我有还没有找到一个感觉足够的,我探索的一些选择是:

  1. Storing the data in a dictionary and storing that in a model as per the answers in Here根据此处的答案,将数据存储在字典中并将其存储在 model 中

  2. Having separate tables and somehow calling the models later when storing data using the method here , in addition to some system with inheritance such as discussed here除了使用此处讨论的 inheritance 的某些系统之外,在使用此处的方法存储数据时,具有单独的表并稍后以某种方式调用模型

  3. Something similar to this using dynamic fields使用动态字段与此类似的东西

I am currently using mySQL, and I saw some other solutions that might work using PostgreSQL for example, but I would like to avoid switching databases if there is another option.我目前正在使用 mySQL,并且我看到了一些其他解决方案,例如可能使用 PostgreSQL,但如果有其他选项,我想避免切换数据库。

I just feel there must be a better more clean solution for this, as it feels like a straightforward implementation.我只是觉得必须有一个更好更干净的解决方案,因为它感觉就像一个简单的实现。 In a sense most of those implementation either feel too hack-y and inefficient especially considering that I will be storing a large amount of data (A larger number of sensors sending data continuously to the server), or do not allow queries on the sub-data.从某种意义上说,这些实现中的大多数要么感觉太老套且效率低下,特别是考虑到我将存储大量数据(大量传感器连续向服务器发送数据),或者不允许对子查询进行查询数据。

Are there any other better implementations for my application?我的应用程序还有其他更好的实现吗? If not, which one of those implementations is best for me to focus on?如果不是,那么这些实现中的哪一个最适合我关注?

The method I ended up using, if anyone faces a similar issue, is as follows:如果有人遇到类似问题,我最终使用的方法如下:

The main model:主model:

class Sensor(Device):
    sensorName = models.CharField(max_length=50)
    sensorSerial = models.CharField(max_length=50)
    sensorTable = models.CharField(max_length=50)

Each sensor will recieve data, each entry of said data will be saved as follows:每个传感器将接收数据,每个数据条目将按如下方式保存:

class Entry(models.Model):
    time = models.DateTimeField()
    sensor = models.ForeignKey(Sensor, on_delete=CASCADE)

And each entry will contain a number of fields which are unknown, depending on what kind of sensor it was, so a third class for the DATA is included and used a pseudo-dictionary:每个条目将包含许多未知的字段,具体取决于它是哪种传感器,因此包含用于 DATA 的第三个 class 并使用伪字典:

class Data(models.Model):
    entry = models.ForeignKey(Entry, on_delete=CASCADE)
    key = models.CharField(max_length=50)
    value = models.CharField(max_length=50)

I am still investigating how to push the effieciency higher, but this is definitely much faster than looping through entries while decoding XML/JSON over 100s of thousands of entries.我仍在研究如何提高效率,但这绝对比在解码 XML/JSON 超过 100 数千个条目时循环遍历条目要快得多。

I will update the answer with any breakthroughs or updates I find.我会用我发现的任何突破或更新来更新答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM