추상화를 구체화하다
도구: 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)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
다양한 언어의 JSONJSON은 Javascript 표기법을 사용하여 데이터 구조를 레이아웃하는 데이터 형식입니다. 그러나 Javascript가 코드에서 이러한 구조를 나타낼 수 있는 유일한 언어는 아닙니다. 저는 일반적으로 '객체'{}...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.