Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- 강제상속
- html의구조 #태그 #빈태그
- CSS Diner
- css상속속성
- 선택자우선순위
- !important
- CSS border
- css너비설정
- 스타일상속
- css복합선택자
- css
- 선택자게임
- css여백설정
- 가상클래스선택자
- CSS선택자
- css여백
- css테두리
- 속성선택자
- css단위
- margin
- 가상요소선택자
- css설정
- css기본선택자
- padding
- style상속
Archives
- Today
- Total
잊기
[python, django] django(장고) shell(셸)을 이용한 데이터 조작 본문
- python manage.py 명령어 ~
manage.py 파일은 project 생성시 자동생성 되므로, 해당 project에 접속해야 사용 가능
- shell : python 언어를 작성할 수 있는 공간 ( >>>의 역할, 주피터노트북의 shell과 같음)
Shell을 이용한 data 조작
- python manage.py shell : 파이썬 셸을 이용하여 데이터를 간리할 수 있는 API ( django가 제공 )
- exit() : shell 에서 나올 때
python manage.py shell
>>> from polls.models import Question, Choice
# 생성한 polls app 의 Question 과 Choice 클래스 import
>>> from django.utils import timezone
# Question 의 culomn : id(자동생성), question_text, pub_date 가 있으며
# pub_date 에 사용할 timezone 클래스 import
>>> q = Question (question_text = "What's new?", pub_date = timezone.now())
>>> q.save()
>>> Question.objects.all()
# Question 컬럼의 rows를 조회하는 ORM 문법
<QuerySet [<Question: 취미가 므야?>, <Question: What's new?>]> # 출력문
>>> Question.objects.all()[:3]
# 건수 제한
- select ( filter )의 다양한 활용
# filter 의 활용
# QuerySet - select 문장 + ( ( where의 역할 )을 하는 filter 사용가능)
>>> Question.objects.filter(
... question_text__startswith='What' # 'What'으로 시작하는 row 검색
... ).exclude(
... pub_date__gte=datetime.date.today()
... ).filter(
... pub_date__gte=fromisoformat('2002, 1, 30')
... )
Question.objects.filter(
question_text__contains = "do"
)
# 'do'를 포함한 row 조회
one_entry = Question.objects.get(pk=1)
one_entry
# pk=1 인 one_entry만 조회
from djangto.utils import timezone
current_year = timezone.now().year
Question.objects.filter(pub_date__year = current_year)
# 생성일자가 올해 인 데이터 조회
q = Question.objects.get(pk=2)
q.choice_set.all()
# choice_set.all() : Question pk=2로 연결되어있는 choice column의 데이터 조회
q.choice_set.count()
# choice_set.all() : Question pk=2로 연결되어있는 choice column의 데이터의 갯수
- 데이터 수정(update) , 저장(save), 조회
q = Question.objects.get(pk=2)
q
# pk = 2인 row 조회
q.question_text = ' What is your favorit hobby?' # 내용 수정
q.save()
q
- 데이터 삭제
>>> Question.objects.filter(question_text__contains='new') # 조회, 출력 (하단)
<QuerySet [<Question: What's new?>]>
>>> Question.objects.filter(question_text__contains='new').delete() # 삭제
'Python _ Django' 카테고리의 다른 글
[python, django] django(장고) application 개발(2) View, Template (0) | 2023.02.24 |
---|---|
[python, django] django(장고) project 생성, application 개발(1) (0) | 2023.02.22 |
[python, django] django (장고) 환경 설정, 가상 환경 설정 (0) | 2023.02.22 |