Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何修复AttributeError:';比如';对象没有属性';过滤器';_Python_Django - Fatal编程技术网

Python 如何修复AttributeError:';比如';对象没有属性';过滤器';

Python 如何修复AttributeError:';比如';对象没有属性';过滤器';,python,django,Python,Django,我试图在我的通知中添加一个条件,以检查用户对与他相关的所有帖子的喜欢总数,并在达到一定数量时发送通知。我已经过滤了帖子作者的全部喜欢,但我一直收到AttributeError:'Like'对象没有属性'filter' 总结一下,这里是post模型 class Post(models.Model): author = models.ForeignKey( User, on_delete=models.CASCADE, related_name='author') n

我试图在我的通知中添加一个条件,以检查用户对与他相关的所有帖子的喜欢总数,并在达到一定数量时发送通知。我已经过滤了帖子作者的全部喜欢,但我一直收到
AttributeError:'Like'对象没有属性'filter'

总结一下,这里是post模型

class Post(models.Model):
    author = models.ForeignKey(
        User, on_delete=models.CASCADE, related_name='author')
    num_likes = models.IntegerField(default=0, verbose_name='No. of Likes')
    likes = models.ManyToManyField(User, related_name='liked', blank=True)
这是喜欢的模型

class Like(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    post = models.ForeignKey(Post, on_delete=models.CASCADE)
    value = models.CharField(choices=LIKE_CHOICES,
                             default='Like', max_length=8)
    def like_progress(sender, instance, *args, **kwargs):
        like = instance.filter(post__author=Post.author).count()
        post = like.post
        sender = like.user

        if like == 12:
            notify = Notification(post=post, sender=sender,
                                  user=post.author, notification_type=3)
            notify.save()

post_save.connect(Like.like_progress, sender=Like)
我的问题:如何修复此错误?

like = instance.filter(post__author=Post.author).count()
您应该这样使用
过滤器

like = Like.objects.filter(post__author=post.author).count() 
#note that it should be post.author not Post.author
这将消除您当前的错误,但我怀疑您能否实现您想要的。我猜您要做的是,每当保存了一个相似的实例时,您都要计算post.author收到的CONT数,如果like数为12,则发送通知


但是,
like\u progress
是一种方法,您是否要通过
like.like\u progress
调用它?

问题是您在实例上调用模型的函数。我建议你读一下这本书

基本上,发生此错误是因为可以从模型的或现有的中使用
filter()
函数,并且您正在从模型的实例中使用它

下面是一个简单的例子,让它更清楚:

这是一个查询集,您可以通过使用模型的管理器(也称为
对象
属性)获得一个查询集

from myapp.models import MyModel

queryset = MyModel.objects.filter(my_attribute=True)
生成的查询集可以进一步过滤

queryset2 = queryset.filter(some_other_attribute=False)
现在简单地说,queryset可以看作是一个实例列表。在本例中,从
MyModel
的模型中,您可以从queryset获取任何实例,或者迭代queryset并遍历所有实例

instance_1 = queryset2.first()
instance_2 = queryset2.last()

# or

for instance in queryset2:
    print('This is an instance', instance)
实例没有与管理器或查询集相同的
filter()
功能,因为它们是无法进一步筛选的单个元素

因此,这将引发AttributeError

try:
    instance_1.filter(my_attribute=False)
except AttributeError:
    pritn("This won't work!")
因此,说到您的示例,正确的方法应该如下所示:

def like_progress(sender, instance, created=None, *args, **kwargs):
        # You may want to stop this if the objects was updated and not created.
        if not created:
            return
        # get all likes from the author
        qs_likes = Like.objects.filter(post__author=instance.author)
        likes_count = qs_likes.count()
        # You can get the post and the sender from the current instance
        post = instance.post
        sender = instance.user

        if likes_count == 12:
            notify = Notification.objects.create(post=post, sender=sender,
                                                 user=post.author, notification_type=3)

另一方面,您不应该将模型的函数与信号连接起来,因为它们确实以不同的顺序接收不同的参数。因此,只需在模型外部定义
like\u progress

我正在为它使用信号
post\u save.connect(like.like\u progress,sender=like)
首先当我使用你的代码时
post
not
post
我收到
UnboundLocalError:local变量'post'在赋值之前被引用
当我将其修改为
post
时,我收到
TypeError:Field'id'需要一个数字,但得到了。
我认为如果你用不同的标题和名称来询问会更好格式。您将询问如何实现like通知功能,并显示所有相关的模型和视图,以及您迄今为止所做的尝试。我记得你之前有过关于这个的问题……是的,它在视图部分,但这是与通知相关的另一部分,当我试图将它设置为一个函数时,当达到一定数量的总喜欢并发生通知时