-1

I'm trying to set my custom manager dynamically for given models.

Here is a sample of my code

class CustomManager(models.Manager):
    def active(self):
        return self.filter(name__startswith='c')


def set_custom_manager(model_class, manager=CustomManager(), manager_name='objects'):
    setattr(model_class, manager_name, manager)
    return model_class


class Facility(BaseModel):
    name = models.CharField(_("Name"), max_length=255)
    icon = models.ImageField(upload_to='location/facilities/')
    order = models.PositiveIntegerField(_("Order"), default=0)

    class Meta:
        verbose_name = _("Facility")
        verbose_name_plural = _("Facilities")
        ordering = ['order', 'id']

    def __str__(self):
        return self.name

set_custom_manager(Facility, CustomManager())

If I check manager class name of Facility instance, it's returning true

>>> Facility.objects.__class__
<class 'apps.chargers.models.CustomManager'>

But if I try to use default methods of models.Manager, it's returning error

Facility.objects.all()
Traceback (most recent call last):
  File "/opt/homebrew/Cellar/[email protected]/3.10.14/Frameworks/Python.framework/Versions/3.10/lib/python3.10/code.py", line 90, in runcode
    exec(code, self.locals)
  File "<console>", line 1, in <module>
  File "/Users/njr/PycharmProjects/iwatt-backend/venv/lib/python3.10/site-packages/django/db/models/query.py", line 376, in __repr__
    data = list(self[: REPR_OUTPUT_SIZE + 1])
  File "/Users/njr/PycharmProjects/iwatt-backend/venv/lib/python3.10/site-packages/django/db/models/query.py", line 400, in __iter__
    self._fetch_all()
  File "/Users/njr/PycharmProjects/iwatt-backend/venv/lib/python3.10/site-packages/django/db/models/query.py", line 1928, in _fetch_all
    self._result_cache = list(self._iterable_class(self))
  File "/Users/njr/PycharmProjects/iwatt-backend/venv/lib/python3.10/site-packages/django/db/models/query.py", line 99, in __iter__
    model_cls = klass_info["model"]
TypeError: 'NoneType' object is not subscriptable
4
  • In your set_custom_manager add the following and check, if you still get the error manager.contribute_to_class(model_class, manager_name) Commented Sep 14, 2024 at 22:28
  • @KhanAsfiReza it works, thank you) Commented Sep 15, 2024 at 0:46
  • I am adding it as an answer please approve it, thank you Commented Sep 15, 2024 at 10:46
  • Exactly what are you trying to do? Using a method to monkey patch is probably not a good idea. Commented Sep 15, 2024 at 17:17

1 Answer 1

1

In your set_custom_manager add the following and check, if you still get the error

manager.contribute_to_class(model_class, manager_name)
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.