Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account.save() fails in Django 4.2 #179

Open
robertgalloway-nobleai opened this issue Apr 16, 2024 · 0 comments · May be fixed by #180
Open

Account.save() fails in Django 4.2 #179

robertgalloway-nobleai opened this issue Apr 16, 2024 · 0 comments · May be fixed by #180

Comments

@robertgalloway-nobleai
Copy link

In Django 4.2, calling manager methods on non-persisted instances is no longer allowed. This change causes Account.save() to fail on any Account that hasn't been previously saved, because the Account._balance() method calls self.transactions.aggregate() before the Account is in the database:

  File ".../helpers.py", line 320, in create_account
    account_type.accounts.create(
  File ".../lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 799, in create
    return super(RelatedManager, self.db_manager(db)).create(**kwargs)
  File ".../lib/python3.8/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File ".../lib/python3.8/site-packages/django/db/models/query.py", line 658, in create
    obj.save(force_insert=True, using=self.db)
  File ".../lib/python3.8/site-packages/oscar_accounts/abstract_models.py", line 149, in save
    self.balance = self._balance()
  File ".../lib/python3.8/site-packages/oscar_accounts/abstract_models.py", line 153, in _balance
    aggregates = self.transactions.aggregate(sum=Sum('amount'))
  File ".../lib/python3.8/site-packages/django/db/models/manager.py", line 87, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File ".../lib/python3.8/site-packages/django/db/models/fields/related_descriptors.py", line 718, in get_queryset
    raise ValueError(
ValueError: 'Account' instance needs to have a primary key value before this relationship can be used.
@robertgalloway-nobleai robertgalloway-nobleai linked a pull request Apr 16, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant