• Home
  • About
  • Posts
  • 1. Fixtures

    • 1.1. The Definition of Fixtures
    • 1.2. The Default Path of Fixtures
    • 1.3. Create Fixtures
    • 1.4. Import Fixtures

Django Fixtures

📅 2022-11-08
🖋️ Byongho96
  • 1. Fixtures
    • 1.1. The Definition of Fixtures
    • 1.2. The Default Path of Fixtures
    • 1.3. Create Fixtures
    • 1.4. Import Fixtures

1. Fixtures

How to share theinitial data during the development phase.

1.1. The Definition of Fixtures

The collection of data that Django knows how to import into the database.

1.2. The Default Path of Fixtures

app/fixtures/
By default, Django looks in the fixtures directory inside each app for fixtures. You can set the FIXTURE_DIRS setting to a list of additional directories where Django should look.

Based on my experience, the BASE_DIR is also one of the default paths.

1.3. Create Fixtures

  • dumpdata
    • python -Xutf8 manage.py dumpdata --indent 4 AppName.ModelName > FileName.json
      • Ex. python -Xutf8 manage.py dumpdata --indent 4 articles.article > articles.json
      • -Xutf8 is for UTF8 format. It should be included if your data has korean letters.
      • --indent 4 is optional, just for making the file look good.
  • Move the fixtures into app_name/fixtures/
    • If the file name is duplicated, you should divide the name space of fixtrues
      • app_name/fixtures/app_name/

1.4. Import Fixtures

  • loaddata
    Considering the dependence between models, independent models should be loaded first.

    • python manage.py loaddata FileName.json

    • If the namespace is divided

      • python manage.py loaddata AppName/FileName.json
    • To avoid considering model relationships

      • python manage.py loaddata FileName1.json FileName2.json FileName3.json
이전 포스트

Django CRUD with Model Form

다음 포스트

Django N:1 Realationship

작성자 프로필
전체 글 (127)
  • Animation
    • Backend
      • Django
      • Spring
    • DevOps
      • AWS
      • CI&CD
      • Docker
      • Git
      • Gunicorn
      • Kubernetes
      • Nginx
    • Frontend
      • Gatsby
      • React
      • Vue
    • Knowledge
      • .etc
      • Algorithm
      • Data Structure
      • Database
      • Design Pattern
      • Interview
      • Network
      • Web
    • Language
      • CSS
      • HTML
      • Java
      • JavaScript
      • Linux
      • Python

    Copyright © 2023 Byongho96  & Powered by Gatsby