arrobalytics / django-ledger

Django Ledger is a double entry accounting system built on the Django Web Framework.
https://www.djangoledger.com
GNU General Public License v3.0
1.03k stars 224 forks source link

AttributeError: 'RelatedManager' object has no attribute 'posted' on account detailed view #212

Closed sinavir closed 3 months ago

sinavir commented 4 months ago

Describe the bug Account detail view is not working with the following error:

Environment:

Request Method: GET
Request URL: http://127.0.0.1:8000/ledger/account/test-0ysguh7o/detail/cecfcfb8-f1e8-4965-8d44-754181f66552/month/2024/7/

Django Version: 5.0.5
Python Version: 3.11.9
Installed Applications:
['django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles',
 'django_ledger',
 'debug_toolbar',
 'django_browser_reload']
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',
 'debug_toolbar.middleware.DebugToolbarMiddleware',
 'django_browser_reload.middleware.BrowserReloadMiddleware']

Traceback (most recent call last):
  File "/nix/store/7cyqw5ylb45czzcy9mf5qlpxxcdrnf01-python3-3.11.9-env/lib/python3.11/site-packages/django/core/handlers/exception.py", line 55, in inner
    response = get_response(request)
               ^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/7cyqw5ylb45czzcy9mf5qlpxxcdrnf01-python3-3.11.9-env/lib/python3.11/site-packages/django/core/handlers/base.py", line 197, in _get_response
    response = wrapped_callback(request, *callback_args, **callback_kwargs)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/7cyqw5ylb45czzcy9mf5qlpxxcdrnf01-python3-3.11.9-env/lib/python3.11/site-packages/django/views/generic/base.py", line 104, in view
    return self.dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/7cyqw5ylb45czzcy9mf5qlpxxcdrnf01-python3-3.11.9-env/lib/python3.11/site-packages/django/contrib/auth/mixins.py", line 73, in dispatch
    return super().dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/7cyqw5ylb45czzcy9mf5qlpxxcdrnf01-python3-3.11.9-env/lib/python3.11/site-packages/django/contrib/auth/mixins.py", line 109, in dispatch
    return super().dispatch(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/7cyqw5ylb45czzcy9mf5qlpxxcdrnf01-python3-3.11.9-env/lib/python3.11/site-packages/django/views/generic/base.py", line 143, in dispatch
    return handler(request, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/7cyqw5ylb45czzcy9mf5qlpxxcdrnf01-python3-3.11.9-env/lib/python3.11/site-packages/django/views/generic/detail.py", line 109, in get
    context = self.get_context_data(object=self.object)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/7cyqw5ylb45czzcy9mf5qlpxxcdrnf01-python3-3.11.9-env/lib/python3.11/site-packages/django_ledger/views/mixins.py", line 210, in get_context_data
    context = super(MonthlyReportMixIn, self).get_context_data(**kwargs)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/nix/store/7cyqw5ylb45czzcy9mf5qlpxxcdrnf01-python3-3.11.9-env/lib/python3.11/site-packages/django_ledger/views/account.py", line 213, in get_context_data
    txs_qs = account_model.transactionmodel_set.posted().order_by(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Exception Type: AttributeError at /ledger/account/test-0ysguh7o/detail/cecfcfb8-f1e8-4965-8d44-754181f66552/month/2024/7/
Exception Value: 'RelatedManager' object has no attribute 'posted'

To Reproduce Steps to reproduce the behavior:

  1. Create test entity with sample data
  2. Click on Chart of Accounts > Accounts > Actions > Details
  3. Get the error

Expected behavior

Working view

Additional context

I use the python packages from my distro (NixOS). This may be the issue can you give me information on whether you achieve to reproduce the bug ?

sinavir commented 4 months ago
diff --git a/django_ledger/views/account.py b/django_ledger/views/account.py
index 4655250..679f061 100644
--- a/django_ledger/views/account.py
+++ b/django_ledger/views/account.py
@@ -210,7 +210,7 @@ class AccountModelYearDetailView(DjangoLedgerSecurityMixIn,
         context['header_title'] = f'Account {account.code} - {account.name}'
         context['page_title'] = f'Account {account.code} - {account.name}'
         account_model: AccountModel = self.object
-        txs_qs = account_model.transactionmodel_set.posted().order_by(
+        txs_qs = account_model.transactionmodel_set(manager="objects").all().posted().order_by(
             'journal_entry__timestamp').select_related(
             'journal_entry', 'journal_entry__entity_unit')
         txs_qs = txs_qs.from_date(self.get_from_date())

This is a quick and dirty fix. A best way would be to dig into the QuerySet/Manager logic but I'm not very used to this part of django framework

25-do commented 4 months ago

Got the same error i think the error 'RelatedManager' object has no attribute 'posted' is occurring when you try to call the method posted() directly on a related manager that is transactionmodel_set without first accessing a queryset instance.

25-do commented 4 months ago

i have opened a pull request for this https://github.com/arrobalytics/django-ledger/pull/213#issue-2402980383

elarroba commented 4 months ago

@25-do @sinavir Thanks for reporting this issue... I'll be addressing this on the next version

elarroba commented 3 months ago

Issue resolved by #214