脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - django数据库迁移migration实现

django数据库迁移migration实现

2022-10-09 13:11苗苗大佬 Python

这篇文章主要介绍了django数据库迁移migration实现,迁移任务是根据对models.py文件的改动情况,添加或者删除表和列,下面详细的相关内容需要的小伙伴可以参考一下

django中,ORM(对象关系映射器—object-relational mapper)任务是:模型化数据库,创建数据库由另外一个系统负责(迁移–migration),迁移任务是根据对models.py文件的改动情况,添加或者删除表和列

django数据库迁移migration实现

依然报错:

django数据库迁移migration实现

django数据库迁移migration实现

models.py

from django.db import models
class Item(models.Model):
    text=models.TextField(default="")

tests.py

"""from django.test import TestCase

# Create your tests here.
class Smokeclass(TestCase):
    def test_bad_maths(self):
        self.assertEquals(1+1,3)"""""
from  django.urls import  resolve
from  django.test import  TestCase
from lists.views import  home_page
from django.http import  HttpRequest
from lists.models import Item
class HomePageTest(TestCase):
    def test_root_url_resolve_to_home_page_view(self):
        found=resolve("/")
        # resolve函数是django内部使用的函数,用于解析url,
        # 并且将其映射到相应的视图函数上,检查网站根路径时"/",
        # 是否能找到home_page函数
        self.assertEquals(found.func,home_page)
    def test_home_page_returns_correct_html(self):
        request=HttpRequest()
        # 创建httprequest对象,用户在浏览器中请求网页时
        # django看到的就是httprequest对象

        response=home_page(request)
        # 把对象传给home_page视图

        html=response.content.decode("utf8")
        # 提取content,得到结果是原始的字节,即发个用户
        # 浏览器的0和1,随后调用.decode(),把原始字节
        # 转换成发给用户的html字符串

        self.assertTrue(html.startswith("<html>"))

        self.assertIn("<title>To-Do lists</title>",html)

        self.assertTrue(html.endswith("</html>))

 self.assertTemplateUsed(response,"home.html")
    def test_user_home_template(self):
        response=self.client.get("/")
        self.assertTemplateUsed(response,"home.html")
    def test_can_save_a_POST_request(self):
        response=self.client.post("/",data={"item_text":"a new list item"})
        self.assertIn("a new list item",response.content.decode())
        self.assertTemplateUsed(response, "home.html")
class ItemModelTest(TestCase):
    def test_saving_and_retrieving_items(self):
        first_item=Item()
        first_item.text="the first list item"
        first_item.save()
        second_item = Item()
        second_item.text = "the second list item"
        second_item.save()
        saved_items=Item.objects.all()
        self.assertEquals(saved_items.count(),2)
        first_saved_item=saved_items[0]
        second_saved_item=saved_items[1]
        self.assertEquals(first_saved_item.text,"the first list item")
        self.assertEquals(second_saved_item.text, "the second list item")
python manage.py makemigrations

到此这篇关于django数据库迁移migration实现的文章就介绍到这了,更多相关django数据库迁移内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_39349045/article/details/123034530

延伸 · 阅读

精彩推荐