Frankie Ribery
Frankie Ribery

Reputation: 12153

Why can't I utilize other model objects in my custom Manager?

I want to implement a custom django.db.models.manager.Manager (let's call it MyManager) for MyModel.

The methods in MyManager needs to invoke filter methods on AnotherModel.

Is this possible ? I'm getting an ImportError because of this.

Upvotes: 1

Views: 57

Answers (1)

lprsd
lprsd

Reputation: 87095

In your MyModel, you need to add your MyManager as an explicit manager.

class MyModel(models.Model):
    objects = MyManager()

You can retain the standard Manager and have your manager both, by including this manager by another name.

class MyModel(models.Model):
    myobjects = MyManager()

If you are using the django-admin, there are nuances involved in what manager's objects are picked up. You can find those and many other details from the awesome django documentation.

Upvotes: 1

Related Questions