class UserInfo(models.Model):
name=models.CharField(max_length=64)
age=models.IntergerField()
python manage.py makemigrations
python manage.py migrate
#setting.py 中INSTALLED_APPS添加app
1.2.增删改查
增
models.UserInfo.object.create(name=‘root‘,age=‘22‘)
dic={‘name‘:‘root‘,‘age‘:22}
models.UserInfo.object.create(**dic)
obj=models.UserInfo(name=‘root‘,age=‘22‘)
obj.save
-----
删
models.UserInfo.object.filter(id=1).delete()
-----
改
models.UserInfo.object.filter(id__gt=1).update(name=‘root1‘)
-----
查
models.UserInfo.object.filter(id=1)
models.UserInfo.object.filter(id__gt=1) #大于
models.UserInfo.object.filter(id__gte=1) #大于等于
models.UserInfo.object.filter(id__lt=1) #小于
models.UserInfo.object.filter(id__lte=1) #小于等于
1.3外键关联
class UserType(models.Model):
caption=models.CharField(max_length=32)
class UserInfo(models.Model):
name=models.CharField(max_length=64)
age=models.IntergerField()
user_type=models.ForeignKey("UserType",to_field=‘id‘) #外键约束
原文:http://blog.51cto.com/13803166/2160351