Trying to know how many rows I have from the different fleets in django

Lleims

I'm working with a project that has 3 models.

  • Devices: All the devices involved in the project.
  • Fleets: Each device is in one fleet.
  • Data: The data that the devices send.

Now I want to know how much data have come me today.

In my view I have this,

def dataPlots(request):
    today = datetime.date(2020, 5, 18)
    data = DevData.objects.filter(data_timestamp__date=today)
    data  = loads(serializers.serialize('json', data))

If I use len() I can now the total value. But how can I know how much data do I have for each fleet?

I have come to create this loop that would tell me for each device, but what I need is the value for each fleet and furthermore I think I am getting complicated.

data_dict = {}
for d in data:
    if d['fields']['dev_eui'] in data_dict:
        data_dict[d['fields']['dev_eui']] = data_dict[d['fields']['dev_eui']] + 1
    else:
        data_dict[d['fields']['dev_eui']] = 1

print(data_dict)

The models are:

class Fleet(models.Model):
    fleet_id = models.IntegerField(primary_key=True, unique=True)
    fleet_name = models.CharField(max_length=20, unique=True)

class Device(models.Model):
    dev_eui = models.CharField(max_length=16, primary_key=True, unique=True)
    dev_name = models.CharField(max_length=20, unique=True)

    fleet_id = models.ForeignKey(Fleet, on_delete=models.CASCADE)

    def __str__(self):
        return self.dev_eui

class DevData(models.Model):
    data_uuid = models.UUIDField(primary_key=True, default=uuid.uuid1, editable=False)
    data_timestamp = models.DateTimeField()
    data = models.FloatField()

    dev_eui = models.ForeignKey(Device, on_delete=models.CASCADE)

    def __str__(self):
        return self.dev_eui

Can somebody help me? I imagine that combining two models and some filter should suffice, but I don't know how to do it.

Willem Van Onsem

You can annotate the Fleets with the given amount of data, for example:

from django.db.models import Count

Fleet.objects.filter(
    device__devdata__data_timestamp__date=today
).annotate(
    total_data=Count('device__devdata')
)

The Fleet objects that arise from this queryset will have an extra attribute .total_data that contains the total amount of data for today.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Django how to know which button is clicked in case I have many buttons?

From Dev

Trying to delete from Join and I know I have funky spacing and I know I have dup IMEI's

From Dev

How many rows of comment-less code do I have?

From Dev

How does the CPU know how many bytes it should read for the next instruction, considering instructions have different lenghts?

From Dev

how can I know how many rows will update sql affected before I execute it

From Dev

Is there any way to seach for coincidences in Excel, in order to know how many times two different values have the same ID

From Java

How can I dynamically create Java class from Solr db when I don't know how many String fields documents have

From Dev

How can I delete rows that have same information but in different columns?

From Dev

How can I keep elements in different rows to have the same width

From Dev

I don't know how to make as many origin_servers as I need to have

From Dev

I have a list of dates and I want to subtract actual date from each of them to know how many day passed. Is there any fast way to do this?

From Dev

How to exclude rows from a SQL fetch that have a related keyword through a many to many relationship

From Dev

need to know how many connections i have on a specific port. using netstat

From Dev

Count how many rows have the same value but if the ID are different count it separate

From Dev

How to concatenate values from many columns into one column when one doesn't know the number of columns will have

From Dev

Display data from database depending on the how many I have entered

From Dev

I have this question but I don't know how to answer on it " Give two examples of different granularities.?

From Dev

How do i have a different data for every account in Django

From Dev

How can i use for loop in Django to have different titles and texts?

From Dev

I have my modal component in a different file and I'm trying to open it from another component

From Dev

Trying to select rows from different tables

From Dev

How many rows or columns can QTableWidget have?

From Dev

How do I introduce a constraint in django table where values from 2 columns will be unique in a table and other rows can have duplicate pairs?

From Dev

How can I manage feed from many different source?

From Dev

How should I handle filling multiple textboxes when I don't know how many of them will have data?

From Dev

How do I echo rows that have a specific ID in it from Database

From Dev

How to know how many rows/columns are taken in a Tkinter.Frame

From Dev

How to know how many rows of code are there for a CLI shell?

From Dev

How to know how many class elements have been selected on click?

Related Related

  1. 1

    Django how to know which button is clicked in case I have many buttons?

  2. 2

    Trying to delete from Join and I know I have funky spacing and I know I have dup IMEI's

  3. 3

    How many rows of comment-less code do I have?

  4. 4

    How does the CPU know how many bytes it should read for the next instruction, considering instructions have different lenghts?

  5. 5

    how can I know how many rows will update sql affected before I execute it

  6. 6

    Is there any way to seach for coincidences in Excel, in order to know how many times two different values have the same ID

  7. 7

    How can I dynamically create Java class from Solr db when I don't know how many String fields documents have

  8. 8

    How can I delete rows that have same information but in different columns?

  9. 9

    How can I keep elements in different rows to have the same width

  10. 10

    I don't know how to make as many origin_servers as I need to have

  11. 11

    I have a list of dates and I want to subtract actual date from each of them to know how many day passed. Is there any fast way to do this?

  12. 12

    How to exclude rows from a SQL fetch that have a related keyword through a many to many relationship

  13. 13

    need to know how many connections i have on a specific port. using netstat

  14. 14

    Count how many rows have the same value but if the ID are different count it separate

  15. 15

    How to concatenate values from many columns into one column when one doesn't know the number of columns will have

  16. 16

    Display data from database depending on the how many I have entered

  17. 17

    I have this question but I don't know how to answer on it " Give two examples of different granularities.?

  18. 18

    How do i have a different data for every account in Django

  19. 19

    How can i use for loop in Django to have different titles and texts?

  20. 20

    I have my modal component in a different file and I'm trying to open it from another component

  21. 21

    Trying to select rows from different tables

  22. 22

    How many rows or columns can QTableWidget have?

  23. 23

    How do I introduce a constraint in django table where values from 2 columns will be unique in a table and other rows can have duplicate pairs?

  24. 24

    How can I manage feed from many different source?

  25. 25

    How should I handle filling multiple textboxes when I don't know how many of them will have data?

  26. 26

    How do I echo rows that have a specific ID in it from Database

  27. 27

    How to know how many rows/columns are taken in a Tkinter.Frame

  28. 28

    How to know how many rows of code are there for a CLI shell?

  29. 29

    How to know how many class elements have been selected on click?

HotTag

Archive