[英]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?我做错了什么或者我应该在哪里放置这段代码以便我可以正确调用它?
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.