Programming Language/SQLite

SQLite_06)Fixtures

Sergemeow 2023. 4. 12. 14:47
  • Django가 데이터베이스로 가져오는 방법을 알고 있는 데이터 모음
  • 생성(데이터 추출) - dumpdata
  • 로드(데이터 입력) - loaddata

dumpdata

python manage.py dumpdata --indent 4 articles.article > articles.json
... accounts.user ...
... articles.comment ...
  • manage.py와 동일한 위치에 data가 담긴 articles.json 파일이 생성됨
# 3개의 모델을 하나의 json 파일로
python manage.py dumpdata --indent 4 articles.article articles.comment accounts.user > data.json

# 모든 모델을 하나의 json 파일로
python manage.py dumpddata --indent 4 > data.json

loaddata

python manage.py loaddata data.json
  • fixtures 기본 경로
    • app_name/fixtures/
    • Django는 설치된 모든 app의 디렉토리에서 fixtures 폴더 이후의 경로로 fixtures 파일을 찾음
  • loaddata를 한번에 실행하지 않고 하나씩 실행한다면 모델 관계에 따라 순서가 중요할 수 있음

  • encoding codec error trouble shooting
    • python -Xutf8 manage.py dump data ….
    • json 파일을 메모장으로 열고 다른 이름으로 저장, 인코딩은 UTF8

'Programming Language > SQLite' 카테고리의 다른 글

SQLite_05)Foreign Key_3  (0) 2023.04.12
SQLite_04)Foreign Key_2  (0) 2023.04.12
SQLite_03)Foreign Key_1  (0) 2023.04.12
SQLite_02)CRUD  (0) 2023.04.12
SQLite_01)Set up & Basics  (0) 2023.04.12