简体   繁体   English

如何在不删除现有对象的情况下将模型管理器添加到Django模型?

[英]How to add a Model Manager to a Django-Model without deleting the objects that already exist?

the question is pretty explanatory I believe. 我相信这个问题可以解释。 I want to add 2 new managers to a django model. 我想为Django模型添加2个新管理器。 However, if I add these two managers, the objects I currently have in my database are deleted. 但是,如果添加这两个管理器,则我当前在数据库中拥有的对象将被删除。 Is there any way to get around this? 有什么办法可以解决这个问题? Or do I need to simply remake all the objects again? 还是我只需要简单地重新制作所有对象?

Don't worry about it. 不用担心 When you add a new manager, it's a class that extends from models.Manager so, this new class already has all the manager's default methods. 当您添加新管理器时,它是从models.Manager扩展的类,因此,此新类已经具有所有管理器的默认方法。

Remember you can create a custom manager by doing: 请记住,您可以通过执行以下操作来创建自定义管理器:

class MyManager(models.Manager):

    ...

And the add to your models class 然后添加到模型类

class MyModel(models.Model):

    ...
    objects = MyManager()

As you can see, MyManager class extends from models.Manager . 如您所见, MyManager类是从models.Manager扩展的。 You can see docs here 您可以在这里查看文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM