---
title: "Bagaimana menyediakan data awal untuk model"
version: 4.2
locale: id
source: https://docs.djangoproject.com/id/4.2/howto/initial-data/
canonical: https://djangodocs.dev/id/4.2/howto/initial-data/
---
# Bagaimana menyediakan data awal untuk model

Terkadang berguna mengisi terlebih dahulu basisdata anda dengan data berkode keras ketika anda pertama kali mensetel aplikasi. Anda dapat menyediakan data awal dengan perpindahan atau perlengkapan.

## Provide initial data with migrations

To automatically load initial data for an app, create a
[data migration](/id/4.2/topics/migrations/#data-migrations). Migrations are run when setting up the
test database, so the data will be available there, subject to [some
limitations](/id/4.2/topics/testing/overview/#test-case-serialized-rollback).

## Provide data with fixtures

You can also provide data using [fixtures](/id/4.2/topics/db/fixtures/#fixtures-explanation),
however, this data isn't loaded automatically, except if you use
[`TransactionTestCase.fixtures`](/id/4.2/topics/testing/tools/#django.test.TransactionTestCase.fixtures).

Perlengkapan tetap adalah sebuah kumpulan dari data yang Django ketahui bagaimana diimpor kedalam sebuah basisdata. Kebanyakan jalan mudah dari pembuatan perlengkapan tetap jika anda telah mendapatkan beberapa data adalah untuk menggunakan perintah  [`manage.py dumpdata`](/id/4.2/ref/django-admin/#django-admin-dumpdata). Atau, anda dapat menulis perlengkapan tetap dengan tangan; perlengkapan tetap dapat ditulis sebagai dokumen JSON, XML atau YAML (dengan [PyYAML](https://pyyaml.org/) terpasang). [serialization documentation](/id/4.2/topics/serialization/) mempunyai lebih rincian tentang setiap dari ini [serialization formats](/id/4.2/topics/serialization/#serialization-formats) yang didukung.

Sebagai contoh, meskipun, inilah yang perlengkapan untuk model `Person` mungkin terlihat seperti dalam JSON:

```js
[
  {
    "model": "myapp.person",
    "pk": 1,
    "fields": {
      "first_name": "John",
      "last_name": "Lennon"
    }
  },
  {
    "model": "myapp.person",
    "pk": 2,
    "fields": {
      "first_name": "Paul",
      "last_name": "McCartney"
    }
  }
]
```

Dan ini adalah yang perlengkapan tetap sama seperti YAML:

```yaml
- model: myapp.person
  pk: 1
  fields:
    first_name: John
    last_name: Lennon
- model: myapp.person
  pk: 2
  fields:
    first_name: Paul
    last_name: McCartney
```

Anda akan menyimpan data ini dalam sebuah direktori `fixtures` didalam aplikasi anda.

You can load data by calling [`manage.py loaddata`](/id/4.2/ref/django-admin/#django-admin-loaddata)
`<fixturename>`, where `<fixturename>` is the name of the fixture file
you've created. Each time you run [`loaddata`](/id/4.2/ref/django-admin/#django-admin-loaddata), the data will be read
from the fixture and reloaded into the database. Note this means that if you
change one of the rows created by a fixture and then run [`loaddata`](/id/4.2/ref/django-admin/#django-admin-loaddata)
again, you'll wipe out any changes you've made.

### Tell Django where to look for fixture files

By default, Django looks for fixtures in the `fixtures` directory inside each
app for, so the command `loaddata sample` will find the file
`my_app/fixtures/sample.json`. This works with relative paths as well, so
`loaddata my_app/sample` will find the file
`my_app/fixtures/my_app/sample.json`.

Django also looks for fixtures in the list of directories provided in the
[`FIXTURE_DIRS`](/id/4.2/ref/settings/#std-setting-FIXTURE_DIRS) setting.

To completely prevent default search form happening, use an absolute path to
specify the location of your fixture file, e.g. `loaddata /path/to/sample`.

> **Namespace your fixture files**
>
> Django will use the first fixture file it finds whose name matches, so if
> you have fixture files with the same name in different applications, you
> will be unable to distinguish between them in your `loaddata` commands.
> The easiest way to avoid this problem is by *namespacing* your fixture
> files. That is, by putting them inside a directory named for their
> application, as in the relative path example above.

> **See also**
>
> Perlengkapan tetap juga digunakan oleh [testing framework](/id/4.2/topics/testing/tools/#topics-testing-fixtures) untuk membantu mengatur kelesarasan lingkungan percobaan.
