经理不能通过模型​​实例访问 [英] Manager isn't accessible via model instances

查看:163
本文介绍了经理不能通过模型​​实例访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将模型对象实例放在另一个中。我提出这个错误:

 经理不能通过主题实例访问
/ pre>

这是我的模型:

  class forum(models。模型):
#一些属性

类主题(models.Model):
#一些属性

类帖子(models.Model):
#一些属性

def delete(self):
forum = self.topic.forum
super(post,self).delete()
论坛.topic_count = topic.objects.filter(forum = forum).count()

这是我的观点:

  def test(request,post_id):
post = topic.objects.get(id = int(topic_id) )
post.delete()

我得到:

  post.delete()
forum.topic_count = topic.objects.filter(forum = forum).count()
经理isn不能通过主题实例访问


解决方案

当您尝试通过模型实例访问模型的管理器时,会导致该问题。您已经使用小写字母类名。这很难说错误是由访问 Manager 的实例引起的。因为可能导致此错误的其他方案是未知的,我正在进行以下假设:您以某种方式混合了主题变量,以便最终指向主题模型而不是类。



这一行是罪魁祸首:

  forum.topic_count = topic .objects.filter(forum = forum).count()
#^^^^^

您必须使用:

  forum.topic_count = Topic.objects.filter(forum = forum).count()
#^^^^^
#模型,而不是实例。

发生什么问题? 对象是一个管理器,在类级别可用,而不是实例。有关详细信息,请参阅检索对象的文档。货币报价:


经理只能访问 em> ,而不是从模型实例中强制执行表级操作和记录级操作之间的分隔。


(加重)



更新



请参阅下面的@Daniel的评论。这是一个好主意(不,你必须:P)使用类名称的标题。例如主题而不是主题。您的类名称是否引起一些混淆,无论您是指实例还是类。由于管理器无法通过< model>实例是非常具体的,我可以提供一个解决方案。错误总是可能不是很明显。


i'm trying to get model objects instance in another one. And i raise this error :

 Manager isn't accessible via topic instance

Here's my model :

class forum(models.Model):
    # Some attributs

class topic(models.Model):
    # Some attributs

class post(models.Model):
    # Some attributs

    def delete(self):
        forum = self.topic.forum
        super(post, self).delete()
        forum.topic_count = topic.objects.filter(forum = forum).count()

Here's my view :

def test(request, post_id):
    post = topic.objects.get(id = int(topic_id))
    post.delete()

And i get :

post.delete()
forum.topic_count = topic.objects.filter(forum = forum).count()
Manager isn't accessible via topic instances

解决方案

The error in question is caused when you try to access the Manager of a model through an instance of the model. You have used lower case class names. This makes it hard to say if the error is caused by an instance accessing the Manager or not. Since other scenarios that can cause this error are unknown I am proceeding on the assumption that you have somehow mixed up the topic variable so that you end up pointing to an instance of the topic model instead of the class.

This line is the culprit:

forum.topic_count = topic.objects.filter(forum = forum).count()
#                   ^^^^^

You have to use:

forum.topic_count = Topic.objects.filter(forum = forum).count()
#                   ^^^^^
#                   Model, not instance.

What is going wrong? objects is a Manager available at the class level, not to the instances. See the documentation for retrieving objects for details. Money quote:

Managers are accessible only via model classes, rather than from model instances, to enforce a separation between "table-level" operations and "record-level" operations.

(Emphasis added)

Update

See the comments from @Daniel below. It is a good idea (nay, you MUST :P) to use title case for class names. For instance Topic instead of topic. Your class names cause some confusion whether you are referring to an instance or a class. Since the Manager isn't accessible via <model> instances is very specific I am able to offer a solution.The error may not be so self evident always.

这篇关于经理不能通过模型​​实例访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆