简体   繁体   English

AttributeError: 'Manager' 对象没有属性

[英]AttributeError: 'Manager' object has no attribute

I'm using Python 3.7.我正在使用 Python 3.7。 I'm having a lot of trouble figuring out how and where I should put a method that creates and saves multiple objects.我在弄清楚应该如何以及在哪里放置一个创建和保存多个对象的方法时遇到了很多麻烦。 I have this in my models.py file我的models.py文件中有这个

class ArticleStatManager(models.Manager):
    def save_main_article(self, article):

Then in another part of the code, I call然后在代码的另一部分,我调用

ArticleStat.objects.save_main_article(article)

but this results in the error但这会导致错误

    ArticleStat.objects.save_main_article(article)
AttributeError: 'Manager' object has no attribute 'save_main_article'

What am I doing wrong or where should I be placing this code so that I can invoke it properly?我做错了什么或者我应该在哪里放置这段代码以便我可以正确调用它?

You need to tell the model to use your custom model manager.您需要告诉模型使用您的自定义模型管理器。

class ArticleStat(models.Model):
    objects = ArticleStatManager()

(DOCS) (文档)

As @DanielRosman point out in his comment, you have to set ArticleStatManager as default manager (you want it through attribute objects , so it has to be the default manager)正如@DanielRosman 在他的评论中指出的那样,您必须将ArticleStatManager设置为默认管理器(您希望它通过属性objects ,因此它必须是默认管理器)

class ArticleStat(models.Model):
    objects = ArticleStatManager()

You could also set a different manager just for that purpose:您还可以为此目的设置不同的经理:

class ArticleStat(models.Model):
    objects = Manager()
    articlesmanager = ArticleStatManager()

and then you could:然后你可以:

ArticleStat.articlesmanager.save_main_article(article)

暂无
暂无

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

相关问题 Kivy - AttributeError: 'DialogContent' object 没有属性 'manager' - Kivy - AttributeError: 'DialogContent' object has no attribute 'manager' Django错误:“管理器”对象上的AttributeError没有属性“ create_user” - Django error: AttributeError at 'Manager' object has no attribute 'create_user' “AttributeError: 'NoneType' 对象没有属性 'test'” 使用上下文管理器 - “AttributeError: 'NoneType' object has no attribute 'test'” using context manager 出现错误:AttributeError: 'Flask' 对象没有属性 'login_manager' - Getting an error: AttributeError: 'Flask' object has no attribute 'login_manager' Django 2.0.3 AttributeError:'Manager'对象没有属性'objects' - Django 2.0.3 AttributeError: 'Manager' object has no attribute 'objects' 屏幕管理器(AttributeError:“ super”对象没有属性“ __getattr__”) - screen manager (AttributeError: 'super' object has no attribute '__getattr__') AttributeError:测试spynner时,“浏览器”对象没有属性“管理器” - AttributeError: 'Browser' object has no attribute 'manager' when test spynner 屏幕管理器AttributeError:“超级”对象没有属性“ __getattr__” - Screen manager AttributeError: 'super' object has no attribute '__getattr__' Django AttributeError:“ Manager”对象没有属性“ select_for_update” - Django AttributeError: 'Manager' object has no attribute 'select_for_update' AttributeError: 'Flask' object 没有属性 'login_manager' - AttributeError: 'Flask' object has no attribute 'login_manager'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM