F
F
Friend2020-01-30 18:46:39
Django
Friend, 2020-01-30 18:46:39

How to fetch data in date range?

I use Django Rest API, before serialization, I need to pull data from the database in the date range from "2020-01-05" to "2020-01-28".

class User(models.Model):
    first_name = models.CharField(max_length=50)
    last_name = models.CharField(max_length=50)


class Statistic(models.Model):
    user_id = models.ForeignKey(User, on_delete=models.CASCADE, related_name='statistic')
    date = models.DateField()
    page_views = models.IntegerField()

I can pull out the statistics in the range
User.objects.get(pk=pk).statistic.exclude(date__gte='2020-01-18').filter(date__gte='2019-01-08')

It gives me only data from the Statistic table, and User as such disappears.
How to get a sample of statistics in the date range in the User object?

Thanks in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2020-01-30
@fox_12

Something like:

User.objects.filter(statistic__date__range=['2019-01-08', '2020-01-18'])

did not try?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question