'Object of type 'type' has no Len()' in Django

Asked

Viewed 88 times

0

I am trying to filter the data from a list between two dates and I received this error.

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

@api_view(('GET',))
@renderer_classes((JSONRenderer))
def DataViewSet (request):
    data_inicio = request.GET.get('inicio')
    data_fim = request.GET.get('fim')
    queryset = Data.objects.filter(time__range=[data_inicio, data_fim])
    #serializer_class = DataSerializer 
    return Response (queryset)

py.

    urlpatterns = [
    path('data/', views.DataViewSet, name='data')
]

When I do the search for example: api/v1/data/? home=2020-12-02%2017:32:20&end=2020-12-03%2023:59:00 it returns me this error 'Object of type 'type' has no Len()', someone knows how to fix it?

Traceback

[01/Feb/2021 14:40:56] "GET /api/v1/data/?inicio=2020-12-02%2017:32:20&fim=2020-12-03%2023:59:00 HTTP/1.1" 500 82584
Internal Server Error: /api/v1/data/
Traceback (most recent call last):
  File "C:\Users\dezab\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\exception.py", line 34, in inner
    response = get_response(request)
  File "C:\Users\dezab\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 115, in _get_response
    response = self.process_exception_by_middleware(e, request)
  File "C:\Users\dezab\AppData\Local\Programs\Python\Python38\lib\site-packages\django\core\handlers\base.py", line 113, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "C:\Users\dezab\AppData\Local\Programs\Python\Python38\lib\site-packages\django\views\decorators\csrf.py", line 54, in wrapped_view
    return view_func(*args, **kwargs)
  File "C:\Users\dezab\AppData\Local\Programs\Python\Python38\lib\site-packages\django\views\generic\base.py", line 71, in view
    return self.dispatch(request, *args, **kwargs)
  File "C:\Users\dezab\AppData\Local\Programs\Python\Python38\lib\site-packages\rest_framework\views.py", line 494, in dispatch
    self.headers = self.default_response_headers  # deprecate?
  File "C:\Users\dezab\AppData\Local\Programs\Python\Python38\lib\site-packages\rest_framework\views.py", line 158, in default_response_headers
    if len(self.renderer_classes) > 1:
TypeError: object of type 'type' has no len()
[01/Feb/2021 14:55:26] "GET /api/v1/data/?inicio=2020-12-02%2017:32:20&fim=2020-12-03%2023:59:00 HTTP/1.1" 500 82719
  • Can you paste all the error stacktrace in your post? In time: I see you are using the suggestion I made in this post. Can you revisit that post and, if relevant, mark the answer as correct? Pay attention to Nota Os valores são recebidos como string e devem ser convertidos para o formato desejado caso necessário

  • 1

    I voted for your two responses in the other post, but the platform said that votes below level 15 are not counted. I didn’t understand why.

  • I believe this error is due to the settings of REST_FRAMEWORK. See if this post helps solve

  • I hadn’t really set the Nderer settings, but even after adding them, I still have the same error

  • Strip the two decorators, ie the two lines with @ before function DataViewSet and see if the error changes. Other, look to use the default class and function names. Classes usually use case and Snake case functions. See here

  • The @ before I put because before appeared error '.accepted_renderer not set on Response', and when I shoot they end up having this other error

  • You tried for example /api/v1/data/?inicio=1&fim=2? Try debugging.

  • In this case it returns ["'1' value has an invalid format. It must be in YYYY-MM-DD HH format: MM [:. Ss [uuuuuu]] [TZ]."]

  • You have to debug your system. (i) Print at start and end, (ii) Try to fix the values as, for example, Data.objects.filter(time__range=["2019-10-10", "2020-10-10"]), (iii) use the datetime library to transform the string into a datetime type. I take the opportunity to ask if the project is in Github or in some open repository so that we can look at it as a whole.

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.