Darlyn Vailatti
Darlyn Vailatti

Reputation: 187

Failed to retrieve list of Django objects with models.Manager

I have the following structure scenario in my models.py :

from django.db import models

class SensorManager(models.Manager):
    def create_sensor(self,numero,pinoFisico):
        sensor = self.create(numero = numero,
                             pinoFisico = pinoFisico,
                             ativo      = False)
        return sensor

class Sensor(models.Model):
    numero = models.IntegerField()
    pinoFisico = models.IntegerField()
    ativo = models.BooleanField()
    dataUltimoReconhecimento = models.DateTimeField()
    situacao = None
    moduloSensor = None

    #Manager
    objects = SensorManager()

    def __init__(self):
        self.moduloSensor = ModuloSensor()

and, in views.py file, i have this:

def formSensores(request):
    sensores = Sensor.objects.all()
    print sensores
    return render(request,"speedapp/monitoraSensores.html",{"sensores": sensores})

When I try to use objects in

print sensores

i get the following stack:

[17/Apr/2017 00:38:09] "GET /speedapp/ HTTP/1.1" 200 3649
Internal Server Error: /speedapp/sensores/
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 149, in get_response
    response = self.process_exception_by_middleware(e, request)
  File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 147, in get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/home/pi/Documents/speed_project/speed/speedapp/views.py", line 39, in formSensores
    print sensores
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 234, in __repr__
    data = list(self[:REPR_OUTPUT_SIZE + 1])
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 258, in __iter__
    self._fetch_all()
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 1074, in _fetch_all
    self._result_cache = list(self.iterator())
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/query.py", line 69, in __iter__
    obj = model_cls.from_db(db, init_list, row[model_fields_start:model_fields_end])
  File "/usr/local/lib/python2.7/dist-packages/django/db/models/base.py", line 460, in from_db
    new = cls(*values)
TypeError: __init__() takes exactly 1 argument (6 given)
[17/Apr/2017 00:38:11] "GET /speedapp/sensores/ HTTP/1.1" 500 15557

As the stack, it seems to be a problem at the moment of building the Sensor object by the SensorManager method in the __init__() method in class from_db, where N arguments were expected ... this problem is related to the my Custom Manager SensorManager?

P.S:

  1. This error does not happens for other objects that are also used with the "all()" method through models.Manager, only for this Sensor class

  2. Only happen when i try "use" instance of list objects, get with all() method, for example: print the value of it

  3. I found this and this related questions in my search, But I could not solve my problem with them

Upvotes: 4

Views: 1039

Answers (1)

raiderrobert
raiderrobert

Reputation: 693

So here's the high level answer. The problem is that you were overriding the __init__ method on the model. You really, really should try to avoid that. Here's some documentation on other options; read the big green note: https://docs.djangoproject.com/en/1.11/ref/models/instances/#django.db.models.Model

Here's an excerpt:

You may be tempted to customize the model by overriding the __init__ method...Rather than overriding __init__, try using one of these approaches 1. Add a classmethod on the model class 2. Add a method on a custom manager (usually preferred)

If you absolutely need to override __init__, then don't forget to call super() pass the values down, and let Django do its stuff first.

Upvotes: 3

Related Questions