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:
This error does not happens for other objects that are also used with the "all()" method through
models.Manager, only for thisSensorclassOnly happen when i try "use" instance of list objects, get with
all()method, for example: print the value of itI found this and this related questions in my search, But I could not solve my problem with them
situacaoandmoduloSensortoNone?formSensoresmethod and the exception occurs after callSensor.objects.all()inprint sensores. The same problem happens if I try to simulate directly in the django python shell (python manage.py shell).Sensorclass, the error continues. The database schema is properly updated withmakemigrationsandmigrate. The fieldssituacaoandmoduloSensorare not persisted, so they receive None. I use them in some methods only for support in memory, a similar approach to@Transientfrom Hibernate in Java, but if there is a better solution I'm listening.