【问题标题】:Django queryset - get created object from database and equal the valuesDjango queryset - 从数据库中获取创建的对象并等于值
【发布时间】:2016-03-12 11:45:56
【问题描述】:

我编写了一个简单的 selenium 测试,它填写了提交表单中的所有字段(在现场添加新闻)。以下是其中之一(标题字段):

# type title
    for t in range(2):
        try:
            title = driver.find_element_by_xpath("//*[@id='id_title']")
            print "I found text!"
            title.send_keys("SomeText")
        except NoSuchElementException as e:
            print "I didn't find it!"
    else:
        print "Retry"

它成功了,在 /admin/news/ (Django) 我可以看到我自动填充的新文章。

现在我想检查从这个表单发送的数据是否等于存储在数据库中的数据。

有人会解释如何使用适当的查询集来检索这些数据并打印结果吗?我创建了一个新类,按照逻辑我认为它会像下面这样:

class NewsModelTestCompare(TestCase):
    def test_creating_news(self):
    # getting object
    n = News.objects.get(title="SomeText")

    self.assertEqual(News.objects.get(pk=n.id), n)

【问题讨论】:

    标签: django python-2.7 selenium-webdriver django-queryset


    【解决方案1】:

    要检查数据是否已经在数据库中,您可以首先使用从表单提交的数据查询News模型,并检查数据库是否返回任何结果,例如:

    matching_objects = News.objects.filter(title="SomeText")
    # this means the query returned at least one result
    self.assertNotEqual(matching_objects.count(), 0)
    

    【讨论】:

      猜你喜欢
      • 2018-06-09
      • 1970-01-01
      • 2011-11-27
      • 2012-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-03
      • 2018-11-12
      相关资源
      最近更新 更多