꽁고의 길--3편

1171 단어 DjangoORM
1.ORM1.1. 클래스 및 필드 만들기
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') #    

좋은 웹페이지 즐겨찾기