-1
I have a program where I need to search through GET on Django the data present between two dates (the user will define these dates)
Models py.
class Data(models.Model):
dev = models.ForeignKey(Device, on_delete=models.CASCADE,related_name="data")
voltage = models.CharField(max_length=255)
current = models.CharField(max_length=255)
active_power = models.CharField(max_length=255)
reactive_power = models.CharField(max_length=255)
temperature = models.CharField(max_length=255)
dev_energy = models.CharField(max_length=6)
dev_on = models.BooleanField(default=True)
time = models.DateTimeField(max_length=70)
class Meta:
verbose_name = 'data'
verbose_name_plural = 'datas'
def __str__(self):
return f'Datas'
Views.py
class DataViewSet(ModelViewSet):
#queryset = Data.objects.all()
queryset = Data.objects.filter(time__range=[data_inicio, data_final])
serializer_class = DataSerializer
Urls.py
urlpatterns = [
url(r'data/$', views.Data.as_view(), name='data'),
url(r'data/(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})/$',
views.Data.as_view(), name='filter_data'),
]
I need to adjust the views so that the parameters data_start and data_final are passed through the url, but I only found how to pass only one of the parameters through it. That’s possible?
Data.objects.filter(time__range=(initial data_final)) I put in the data manager view?
– dezaborchardt
Datamanager is part of Model (models.py). Are you looking to form, view and template for data recovery? If so, take a look at it here
– Paulo Marques
Actually I would like to do only View, I’ve been working on Jango for a while and I still haven’t touched the form and template
– dezaborchardt
Look at the link I passed, has the
views.py
there.– Paulo Marques
Thanks for the help, I adjusted the models according to the "elegant" way, but I still could not tidy up the views even with the link I’m having difficulties to apply to my example
– dezaborchardt
I suggest putting all the important files in your project: urls.py, models.py, views.py in a new post and explaining what you want exactly.
– Paulo Marques
For those who voted negative, could you tell me why I should improve the answer? I believe my answer answered the topic "Do a search between two dates in Django"
– Paulo Marques
I edited and placed the views and urls to give an idea of what I need
– dezaborchardt
Responded to comments in a different response.
– Paulo Marques