fix: handle models with empty local_managers (#90)

* Fix value error

* test: add test for models with empty local_managers

* fix: check for local_managers being empty

Co-authored-by: Mike <22396211+Dresdn@users.noreply.github.com>
This commit is contained in:
PUYUP 2021-10-26 20:10:36 +07:00 committed by GitHub
parent a98766fc55
commit 9b5df1c99e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 2 deletions

View file

@ -109,8 +109,13 @@ class Registry(object):
# Save the old manager if the attribute name conflicts with the new one.
if hasattr(self.model_cls, self.config_cls.manager_attr):
mgr = getattr(self.model_cls, self.config_cls.manager_attr)
self.config_cls.old_mgr = mgr
self.model_cls._meta.local_managers.remove(mgr)
# For some models, `local_managers` may be empty, eg.
# django.contrib.auth.models.User and AbstractUser
if mgr in self.model_cls._meta.local_managers:
self.config_cls.old_mgr = mgr
self.model_cls._meta.local_managers.remove(mgr)
self.model_cls._meta._expire_cache()
# Attach the new manager to the model.

View file

@ -1,3 +1,4 @@
from django.contrib.auth.models import User
from django.test import TestCase
import eav
@ -100,3 +101,14 @@ class RegistryTests(TestCase):
@eav.decorators.register_eav()
class Foo(object):
pass
def test_model_without_local_managers(self):
"""Test when a model doesn't have local_managers."""
# Check just in case test model changes in the future
assert bool(User._meta.local_managers) is False
eav.register(User)
assert isinstance(User.objects, eav.managers.EntityManager)
# Reverse check: managers should be empty again
eav.unregister(User)
assert bool(User._meta.local_managers) is False