app下的models.py:
from django.db import models # Create your models here. class UserInfo(models.Model): username = models.CharField(max_length=32) password = models.CharField(max_length=64)
settings.py加上app名:
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'app01', ]
调出Ternimal:
python manage.py makemigrations
python manage.py migrate
就创建好数据库了
增加数据:
def orm(request): models.UserInfo.objects.create( username="root", password='123' )
另一种创建数据方法:
obj = models.UserInfo( username='root1', password='123' ) obj.save()
查询数据:
def orm(request): result = models.UserInfo.objects.all() #这里返回的是一个QuerySet类型 for row in result: print(row.id, row.username, row.password)
按条件查询:
def orm(request): result = models.UserInfo.objects.filter(username='root') #这里返回的是一个QuerySet类型 for row in result: print(row.id, row.username, row.password)
删除全部数据:
models.UserInfo.objects.all().delete()
按条件删除:
models.UserInfo.objects.filter(id=1).delete()
修改全部(更新):
models.UserInfo.objects.all().update(password=666)
更新部分:
models.UserInfo.objects.filter(username='root1').update(password=666)