我如何在Django中测试一个空的查询器? [英] How can i test for an empty queryset in Django?

查看:82
本文介绍了我如何在Django中测试一个空的查询器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Django中测试一个应该从对象中删除所有标签的视图。为此我使用这个断言:

I'm testing a view in Django that should remove all the tags from an object. For that i use this assertion:

self.assertEqual(list(Tag.objects.get_for_object(Animal.objects.get(pk=1))),[])

效果很好,因为我得到一个空列表。我将Django查询器包装在列表中以避免这种情况:

That works well, as i get an empty list in return. I wrapped the Django queryset in a list to avoid this:

AssertionError: [] != []

其中空的Django查询器与空列表进行比较。

where an empty Django queryset is compared with an empty list.

但是,由于这不是我喜欢的东西,我想知道是否有更好的方法来做这个测试。

But as this is not something i like a lot, i wondered if there is a nicer way to do that test.

推荐答案

使用 存在

self.assertFalse(Tag.objects.get_for_object(Animal.objects.get(pk=1)).exists())

这篇关于我如何在Django中测试一个空的查询器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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