django數(shù)據(jù)庫遷移migration實現(xiàn)
在django
中,ORM(對象關(guān)系映射器—object-relational mapper
)任務(wù)是:模型化數(shù)據(jù)庫,創(chuàng)建數(shù)據(jù)庫由另外一個系統(tǒng)負(fù)責(zé)(遷移–migration
),遷移任務(wù)是根據(jù)對models.py
文件的改動情況,添加或者刪除表和列
依然報錯:
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)站根路徑時"/", ? ? ? ? # 是否能找到home_page函數(shù) ? ? ? ? self.assertEquals(found.func,home_page) ? ? def test_home_page_returns_correct_html(self): ? ? ? ? request=HttpRequest() ? ? ? ? # 創(chuàng)建httprequest對象,用戶在瀏覽器中請求網(wǎng)頁時 ? ? ? ? # django看到的就是httprequest對象 ? ? ? ? response=home_page(request) ? ? ? ? # 把對象傳給home_page視圖 ? ? ? ? html=response.content.decode('utf8') ? ? ? ? # 提取content,得到結(jié)果是原始的字節(jié),即發(fā)個用戶 ? ? ? ? # 瀏覽器的0和1,隨后調(diào)用.decode(),把原始字節(jié) ? ? ? ? # 轉(zhuǎn)換成發(fā)給用戶的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ù)庫遷移migration實現(xiàn)的文章就介紹到這了,更多相關(guān)django數(shù)據(jù)庫遷移內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python中openpyxl和xlsxwriter對Excel的操作方法
這篇文章主要介紹了python中openpyxl和xlsxwriter對Excel的操作方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-03-03Python中如何將Tqdm與Asyncio結(jié)合使用呢
這篇文章主要和大家詳細(xì)介紹了在Python中如何將Tqdm與Asyncio結(jié)合使用呢,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-05-05Python實現(xiàn)批量導(dǎo)入1000條xlsx數(shù)據(jù)
本文主要介紹了Python實現(xiàn)批量導(dǎo)入1000條xlsx數(shù)據(jù),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02