为什么我不能在自定义管理器中使用其他模型对象?

时间:2022-09-24 22:50:45

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

我想为MyModel实现一个自定义的django.db.models.manager.Manager(让我们称之为MyManager)。

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

MyManager中的方法需要在AnotherModel上调用过滤器方法。

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

这可能吗 ?因为这个我得到了一个ImportError。

1 个解决方案

#1


1  

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

在MyModel中,您需要将MyManager添加为显式管理器。

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.

如果您使用的是django-admin,那么管理员的对象将被捕获的细微差别。您可以从令人敬畏的django文档中找到这些和许多其他细节。

#1


1  

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

在MyModel中,您需要将MyManager添加为显式管理器。

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.

如果您使用的是django-admin,那么管理员的对象将被捕获的细微差别。您可以从令人敬畏的django文档中找到这些和许多其他细节。