0

I am getting the error Manager isn't accessible via Contact instances

StackTrace

Environment:


Request Method: POST
Request URL: http://localhost:8000/upload/

Django Version: 3.1.7
Python Version: 3.9.1
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'contactapp']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback (most recent call last):
  File "/Users/Me/.local/share/virtualenvs/test_code-Xw7ix4y5/lib/python3.9/site-packages/django/core/handlers/exception.py", line 47, in inner
    response = get_response(request)
  File "/Users/Me/.local/share/virtualenvs/test_code-Xw7ix4y5/lib/python3.9/site-packages/django/core/handlers/base.py", line 181, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
  File "/Users/Me/Data/Clients/BanzaiPythonTest/test_code/contactapp/views.py", line 39, in upload_file
    print(result.get())
  File "/Users/Me/.local/share/virtualenvs/test_code-Xw7ix4y5/lib/python3.9/site-packages/celery/result.py", line 223, in get
    return self.backend.wait_for_pending(
  File "/Users/Me/.local/share/virtualenvs/test_code-Xw7ix4y5/lib/python3.9/site-packages/celery/backends/asynchronous.py", line 201, in wait_for_pending
    return result.maybe_throw(callback=callback, propagate=propagate)
  File "/Users/Me/.local/share/virtualenvs/test_code-Xw7ix4y5/lib/python3.9/site-packages/celery/result.py", line 335, in maybe_throw
    self.throw(value, self._to_remote_traceback(tb))
  File "/Users/Me/.local/share/virtualenvs/test_code-Xw7ix4y5/lib/python3.9/site-packages/celery/result.py", line 328, in throw
    self.on_ready.throw(*args, **kwargs)
  File "/Users/Me/.local/share/virtualenvs/test_code-Xw7ix4y5/lib/python3.9/site-packages/vine/promises.py", line 234, in throw
    reraise(type(exc), exc, tb)
  File "/Users/Me/.local/share/virtualenvs/test_code-Xw7ix4y5/lib/python3.9/site-packages/vine/utils.py", line 30, in reraise
    raise value

Exception Type: AttributeError at /upload/
Exception Value: Manager isn't accessible via Contact instances

I am making a utility function and below is my code:

class Contact(models.Model):
    name = models.CharField(max_length=100)
    phone = models.CharField(max_length=20, null=True)
    email = models.CharField(max_length=256,
                             null=True)  # Ref: https://www.rfc-editor.org/errata_search.php?rfc=3696&eid=1690
    created_at = models.DateTimeField(auto_now_add=True)

    def __repr__(self):
        return self.name

    def get_records_by_duration(self, duration=3):
        minutes_ago = now() + datetime.timedelta(minutes=-duration)
        contact_object = Contact.objects.filter(
                                                (Q(email__in=['adn@mae.com', 'adnan@gmail.com']) | Q(phone__in=[])),
                                                Q(created_at__gte=minutes_ago))
        total = contact_object.count()

        print('Total = {}'.format(total))

In my view I am doing the following:

contact_object = Contact()
    result = contact_object.get_records_by_duration()
Volatil3
  • 14,253
  • 38
  • 134
  • 263
  • Please post the *full* traceback. – Willem Van Onsem Mar 06 '21 at 09:37
  • This does not seem to be related to the code you post, but to the `upload_file` view. – Willem Van Onsem Mar 06 '21 at 09:41
  • @Volatil3 Also Your method `get_records_by_duration` should be a `classmethod`. (You make an empty instance just to access the method, implying you really don't need an instance for the method and actually just want it in the class). Plus I don't see you returning anything in the method yet you write `result = contact_object.get_records_by_duration()`... – Abdul Aziz Barkat Mar 06 '21 at 09:46

1 Answers1

0

contact_object is an instance of Contact class. In django the table-level operations has to be accessed from the class not the instance of the class. So, Contact.get_records_by_duration() should work I think.

Also see here: Manager isn't accessible via model instances

Aditya Jha
  • 21
  • 5