django數(shù)據(jù)庫(kù)遷移migration實(shí)現(xiàn)
在django中,ORM(對(duì)象關(guān)系映射器—object-relational mapper)任務(wù)是:模型化數(shù)據(jù)庫(kù),創(chuàng)建數(shù)據(jù)庫(kù)由另外一個(gè)系統(tǒng)負(fù)責(zé)(遷移–migration),遷移任務(wù)是根據(jù)對(duì)models.py文件的改動(dòng)情況,添加或者刪除表和列

依然報(bào)錯(cuò):


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函數(shù)是django內(nèi)部使用的函數(shù),用于解析url,
? ? ? ? # 并且將其映射到相應(yīng)的視圖函數(shù)上,檢查網(wǎng)站根路徑時(shí)"/",
? ? ? ? # 是否能找到home_page函數(shù)
? ? ? ? self.assertEquals(found.func,home_page)
? ? def test_home_page_returns_correct_html(self):
? ? ? ? request=HttpRequest()
? ? ? ? # 創(chuàng)建httprequest對(duì)象,用戶(hù)在瀏覽器中請(qǐng)求網(wǎng)頁(yè)時(shí)
? ? ? ? # django看到的就是httprequest對(duì)象
? ? ? ? response=home_page(request)
? ? ? ? # 把對(duì)象傳給home_page視圖
? ? ? ? html=response.content.decode('utf8')
? ? ? ? # 提取content,得到結(jié)果是原始的字節(jié),即發(fā)個(gè)用戶(hù)
? ? ? ? # 瀏覽器的0和1,隨后調(diào)用.decode(),把原始字節(jié)
? ? ? ? # 轉(zhuǎn)換成發(fā)給用戶(hù)的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
到此這篇關(guān)于django數(shù)據(jù)庫(kù)遷移migration實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)django數(shù)據(jù)庫(kù)遷移內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
一個(gè)月入門(mén)Python爬蟲(chóng)學(xué)習(xí),輕松爬取大規(guī)模數(shù)據(jù)
利用爬蟲(chóng)我們可以獲取大量的價(jià)值數(shù)據(jù),從而獲得感性認(rèn)識(shí)中不能得到的信息,這篇文章給大家?guī)?lái)了一個(gè)月入門(mén)Python學(xué)習(xí),爬蟲(chóng)輕松爬取大規(guī)模數(shù)據(jù),感興趣的朋友一起看看吧2018-01-01
python?類(lèi)對(duì)象的析構(gòu)釋放代碼演示
這篇文章主要介紹了python?類(lèi)對(duì)象的析構(gòu)釋放代碼演示,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-09-09
python遍歷一個(gè)目錄,輸出所有的文件名的實(shí)例
下面小編就為大家分享一篇python遍歷一個(gè)目錄,輸出所有的文件名的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
在pycharm中文件取消用 pytest模式打開(kāi)的操作
這篇文章主要介紹了在pycharm中文件取消用 pytest模式打開(kāi)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-09-09
2022最新Python日志庫(kù)logging總結(jié)
這篇文章主要介紹了2022最新Python日志庫(kù)logging總結(jié),Python logging 庫(kù)設(shè)計(jì)的真的非常靈活,如果有特殊的需要還可以在這個(gè)基礎(chǔ)的 logging 庫(kù)上進(jìn)行改進(jìn),創(chuàng)建新的 Handler 類(lèi)解決實(shí)際開(kāi)發(fā)中的問(wèn)題,需要的朋友可以參考下2022-05-05
Python pandas對(duì)excel的操作實(shí)現(xiàn)示例
這篇文章主要介紹了Python pandas對(duì)excel的操作實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
Python實(shí)現(xiàn)郵件發(fā)送的詳細(xì)設(shè)置方法(遇到問(wèn)題)
這篇文章主要介紹了Python實(shí)現(xiàn)郵件發(fā)送的詳細(xì)設(shè)置方法(遇到問(wèn)題),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01
python使用裝飾器和線(xiàn)程限制函數(shù)執(zhí)行時(shí)間的方法
這篇文章主要介紹了python使用裝飾器和線(xiàn)程限制函數(shù)執(zhí)行時(shí)間的方法,主要涉及timelimited函數(shù)的使用技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2015-04-04

