4

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

8
  • For debugging purposes, you could try (1) unplugging your custom model manager and (2) ensure you made and ran migrations. Also, could you explain why you have set situacao and moduloSensor to None? Commented Apr 17, 2017 at 3:12
  • Apparently,, this is not your exact code - can you update your question with your exact code (only relevant pieces) Commented Apr 17, 2017 at 4:27
  • @karthikr I agree with you, I think the question can be improved, but it already contains the exact pieces of my code or, relevant pieces. I call the formSensores method and the exception occurs after call Sensor.objects.all() in print sensores. The same problem happens if I try to simulate directly in the django python shell ( python manage.py shell ). Commented Apr 17, 2017 at 14:24
  • @raiderrobert I unplugging the manager, commenting on objects of the Sensor class, the error continues. The database schema is properly updated with makemigrations and migrate. The fields situacao and moduloSensor are not persisted, so they receive None. I use them in some methods only for support in memory, a similar approach to @Transient from Hibernate in Java, but if there is a better solution I'm listening. Commented Apr 17, 2017 at 14:28
  • @DarlynVailatti, so I think you're using the Django 1.9.x branch. Not sure it's very important to the conversation, but I figured I'd note it, and further, that this very section of code got refactored in 1.10: github.com/django/django/blame/1.10/django/db/models/… All that said, I replicated your code, and I had no problem running it. So either you have a perfect storm of circumstances, or you've omitted something inadvertently. Commented Apr 18, 2017 at 2:34

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.