zoukankan      html  css  js  c++  java
  • Django学习系列18:使用迁移创建生产数据库

    Django生成一个很有帮助的错误信息,大意是说没有正确设置数据库。

    你可能会有疑惑,为什么在单元测试一切都运行ok,这是因为Django为单元测试创建了专用的测试数据库——这是Django中Testcase所做的神奇事情之一。

    为了设置好真正的数据库,要创建一个数据库,SQlite数据库只是硬盘中的一个文件,在Django的setting.py文件中发现,默认情况下,Django把数据库保存为db.sqlite3,放在项目的基目录中

    我们在Models.py文件和后来创建的迁移文件中告诉Django创建数据库所需的一切信息,为了创建真正的数据库,要使用Django中另一个强大manage.py命令——migrate

    # python manage.py migrate
    Operations to perform:
      Apply all migrations: admin, auth, contenttypes, lists, sessions
    Running migrations:
      Applying contenttypes.0001_initial... OK
      Applying auth.0001_initial... OK
      Applying admin.0001_initial... OK
      Applying admin.0002_logentry_remove_auto_add... OK
      Applying contenttypes.0002_remove_content_type_name... OK
      Applying auth.0002_alter_permission_name_max_length... OK
      Applying auth.0003_alter_user_email_max_length... OK
      Applying auth.0004_alter_user_username_opts... OK
      Applying auth.0005_alter_user_last_login_null... OK
      Applying auth.0006_require_contenttypes_0002... OK
      Applying auth.0007_alter_validators_add_error_messages... OK
      Applying auth.0008_alter_user_username_max_length... OK
      Applying lists.0001_initial... OK
      Applying lists.0002_item_text... OK
      Applying sessions.0001_initial... OK

    这个时候,刷新localhost上的页面,发现错误页面不见了。然后运行功能测试:

    AssertionError: '1: Buy peacock feathers' not found in ['1:Buy peacock feathers', '1:Buy peacock feathers']

    快成功了,只需要让清单显示正确的序号即可,另一个出色的Django模板标签forloop.counter能帮助解决这个问题。

                {% for item in items %}
                    <tr><td>{{ forloop.counter }}: {{ item.text}}</td></tr>
                {% endfor %}

    再试一次,应该会看到功能测试运行到最后了:

        self.fail("完成测试")
    AssertionError: 完成测试

    不过运行测试时,可能会注意什么不对劲的地方

    上一次运行测试时在数据库中遗留了数据。需要一种自动清理机制,可以手动清理,方法是先删除数据库在执行migrate命令新建。

    # rm db.sqlite3 
    # python manage.py migrate --noinput

    Operations to perform:
      Apply all migrations: admin, auth, contenttypes, lists, sessions
    Running migrations:
      Applying contenttypes.0001_initial... OK
      Applying auth.0001_initial... OK
      Applying admin.0001_initial... OK
      Applying admin.0002_logentry_remove_auto_add... OK
      Applying contenttypes.0002_remove_content_type_name... OK
      Applying auth.0002_alter_permission_name_max_length... OK
      Applying auth.0003_alter_user_email_max_length... OK
      Applying auth.0004_alter_user_username_opts... OK
      Applying auth.0005_alter_user_last_login_null... OK
      Applying auth.0006_require_contenttypes_0002... OK
      Applying auth.0007_alter_validators_add_error_messages... OK
      Applying auth.0008_alter_user_username_max_length... OK
      Applying lists.0001_initial... OK
      Applying lists.0002_item_text... OK
      Applying sessions.0001_initial... OK

    清理之后要确保功能测试能通过。

    除了这个小问题之外,我们的代码基本上可以正常运行了,下面做一次提交

    # git status   # 看见home.html, tests.py, and views.py所做的改动
    # git diff
    #  git add lists
    # git commit -m "Redirect after POST, and show all items in template——发布后重定向,并显示模板中的所有项"
    # git tag end-of-chapter-05

    小结:

    1. 编写一个表单,使用post请求把新待办事项添加到清单中;
    2. 创建了一个简单的数据库模型,用来存储待办事项;
    3. 使用了至少三种功能测试的调试技术

    有用的概念:

    • 回归:新添加的代码破坏了应用原本可以正常使用的功能
    • 意外失败:意味着测试中有错误,或者测试帮我们发现了一个回归,因此要在代码中修正;
    • 遇红、变绿、重构:描述TDD流程的另一种方式;
    • 三角法:
    • 事不过三,三则重构
    • 记录编写代码过程中遇到的问题,等完成手里的工作后,回来再来解决。
  • 相关阅读:
    CDH版本大数据集群下搭建的Hue详细启动步骤(图文详解)
    如何正确且成功破解跨平台数据库管理工具DbVisualizer?(图文详解)
    [转]【HTTP】Fiddler(二)
    [转]jQuery UI Dialog Modal Popup Yes No Confirm example in ASP.Net
    [转]artDialog
    [转]GridView排序——微软提供Sort
    [转]GridView中直接新增行、编辑和删除
    [转]asp.net的ajax以及json
    [转]JQuery Ajax 在asp.net中使用总结
    [转]Jquery easyui开启行编辑模式增删改操作
  • 原文地址:https://www.cnblogs.com/ranxf/p/11692387.html
Copyright © 2011-2022 走看看