程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django database migration

編輯:Python

stay django in ,ORM( Object relational mapper —object-relational mapper) The task is : Model database , Creating the database is the responsibility of another system ( transfer –migration), The migration task is based on models.py Changes to documents , Add or delete tables and columns


Still wrong


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 The function is django Functions used internally , For parsing url,
# And map it to the corresponding view function , When checking the site root path "/",
# Can I find home_page function 
self.assertEquals(found.func,home_page)
def test_home_page_returns_correct_html(self):
request=HttpRequest()
# establish httprequest object , When a user requests a web page in the browser 
# django What I see is httprequest object 
response=home_page(request)
# Pass on the object to home_page View 
html=response.content.decode('utf8')
# extract content, The result is the original byte , Send a user immediately 
# Browser's 0 and 1, Then call .decode(), Original byte 
# Convert it to the... Sent to the user html character string 
self.assertTrue(html.startswith('<html>'))
self.assertIn('<title>To-Do lists</title>',html)
self.assertTrue(html.endswith('</html>'))
def test_home_page_returns_correct_html_chonggou(self):
response=self.client.get('/')
html = response.content.decode('utf8')
# extract content, The result is the original byte , Send a user immediately 
# Browser's 0 and 1, Then call .decode(), Original byte 
# Convert it to the... Sent to the user html character string 
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

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved