추상화를 구체화하다

4736 단어
본고는 추상적인 것을 가능한 한 가시화하는데 기초가 좋지 않은 학생들이 더욱 분명하게 이해하도록 하는 것이 목적이다.이 시리즈의 Django 튜토리얼은 대부분 초보자 튜토리얼에서 나온 것으로 개인적인 이해와 시각화 부분을 추가했다.
도구: MySQL Workbench 6.1.7 CE
데이터베이스 구성:
setting.py DATABASES
    DATABASES = {
    'default': {
    'ENGINE': 'django.db.backends.mysql',
    'NAME': 'test',
    'USER': 'root',
    'PASSWORD':'***',
    'HOST':'127.0.0.1',
    'PORT':'3306',
    }}`

모델 수정.py 파일
# models.py
from django.db import models
class Test(models.Model):
     name = models.CharField(max_length=20)```
               , app  blog,     *blog_test*,    models.Model,               (name),      CharField(   varchar)、DateField(   datetime), max_length       。
-        ,   pycharm           *Terminal*:
`$ python manage.py migrate   #      `
`$ python manage.py makemigrations yourapp.name  #   Django                `
![](http://upload-images.jianshu.io/upload_images/2326415-8c7fa7ab7f73c8e7.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
`$ python manage.py migrate yourapp.name   #      `
![](http://upload-images.jianshu.io/upload_images/2326415-256011a4e11c7360.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
               
![](http://upload-images.jianshu.io/upload_images/2326415-d1cdd94e546d9dc1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
##      
          testdb.py   ,    urls.py:
```javascript
from . import testdb
urlpatterns = {
    url(r'^testdb$', testdb.testdb),
}```
- **    **
         ,      save   ,   SQL  INSERT:
```javascript
#testdb.py
def testdb(request):
    test1 = Test(name = 'vonhehe')
    test1.save()
    return HttpResponse("

")```    : ![](http://upload-images.jianshu.io/upload_images/2326415-109bebed80dd3f6a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)    , blog_test ![](http://upload-images.jianshu.io/upload_images/2326415-8a44d46f9fe7a35a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) - ** ** ```javascript # -*- coding: utf-8 -*- from django.http import HttpResponse from TestModel.models import Test def testdb(request):# # response = "" response1 = "" # objects all() , SQL SELECT * FROM blog_test list = Test.objects.all() # filter SQL WHERE, response2 = Test.objects.filter(id=1) # response3 = Test.objects.get(id=1) # SQL OFFSET 0 LIMIT 2; Test.objects.order_by('name')[0:2] # Test.objects.order_by("id") # Test.objects.filter(name="vonhehe").order_by("id") # for var in list: response1 += var.name + " " response = response1 return HttpResponse("

" + response + "

")``` : ![](http://upload-images.jianshu.io/upload_images/2326415-a107154e8b7586b3.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) - ** ** save() update(): ```javascript # -*- coding: utf-8 -*- from django.http import HttpResponse from TestModel.models import Test # def testdb(request): # id=1 name , save, SQL UPDATE test1 = Test.objects.get(id=1) test1.name = 'Google' test1.save() # #Test.objects.filter(id=1).update(name='Google') # # Test.objects.all().update(name='Google') return HttpResponse("

")``` : ![](http://upload-images.jianshu.io/upload_images/2326415-d54afa30d6727e0f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) : ![](http://upload-images.jianshu.io/upload_images/2326415-f02e1f7881df1f77.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) - ** ** : `Test(name = 'vonhehe').save()` ![](http://upload-images.jianshu.io/upload_images/2326415-c5fd0b401a5053af.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ```javascript # def testdb(request): # id=1 test1 = Test.objects.get(id=1) test1.delete() # # Test.objects.filter(id=1).delete() # # Test.objects.all().delete() return HttpResponse("

")``` : ![](http://upload-images.jianshu.io/upload_images/2326415-2da472ad5f1676ad.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) ,id=1 : ![](http://upload-images.jianshu.io/upload_images/2326415-3ee148e34a948c8b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240) *** --- [【 】](http://www.runoob.com/django/django-model.html)

좋은 웹페이지 즐겨찾기