{"title":"Perpindahan","version":"4.0","locale":"id","docname":"topics/migrations","url":"/id/4.0/topics/migrations/","canonical":"https://djangodocs.dev/id/4.0/topics/migrations/","summary":"Migrations are Django's way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. They're designed to…","html":"<span id=\"migrations\"></span><h1>Perpindahan<a class=\"heading-anchor\" href=\"#module-django.db.migrations\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>Migrations are Django's way of propagating changes you make to your models\n(adding a field, deleting a model, etc.) into your database schema. They're\ndesigned to be mostly automatic, but you'll need to know when to make\nmigrations, when to run them, and the common problems you might run into.</p>\n<section id=\"the-commands\">\n<h2>Perintah<a class=\"heading-anchor\" href=\"#the-commands\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>There are several commands which you will use to interact with migrations\nand Django's handling of database schema:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/id/4.0/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">migrate</span></code></a>, yang bertanggung jawab untuk memberlakukan dan tidak memberlakukan perpindahan.</p></li>\n<li><p><a class=\"reference internal\" href=\"/id/4.0/ref/django-admin/#django-admin-makemigrations\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">makemigrations</span></code></a>, which is responsible for creating new migrations\nbased on the changes you have made to your models.</p></li>\n<li><p><a class=\"reference internal\" href=\"/id/4.0/ref/django-admin/#django-admin-sqlmigrate\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">sqlmigrate</span></code></a>, yang menampilan pernyataan SQL untuk perpindahan.</p></li>\n<li><p><a class=\"reference internal\" href=\"/id/4.0/ref/django-admin/#django-admin-showmigrations\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">showmigrations</span></code></a>, yang menampilan perpindahan proyek dan keadaan mereka.</p></li>\n</ul>\n<p>You should think of migrations as a version control system for your database\nschema. <code class=\"docutils literal notranslate\"><span class=\"pre\">makemigrations</span></code> is responsible for packaging up your model changes\ninto individual migration files - analogous to commits - and <code class=\"docutils literal notranslate\"><span class=\"pre\">migrate</span></code> is\nresponsible for applying those to your database.</p>\n<p>The migration files for each app live in a &quot;migrations&quot; directory inside\nof that app, and are designed to be committed to, and distributed as part\nof, its codebase. You should be making them once on your development machine\nand then running the same migrations on your colleagues' machines, your\nstaging machines, and eventually your production machines.</p>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Catatan</p>\n<p>It is possible to override the name of the package which contains the\nmigrations on a per-app basis by modifying the <a class=\"reference internal\" href=\"/id/4.0/ref/settings/#std-setting-MIGRATION_MODULES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">MIGRATION_MODULES</span></code></a>\nsetting.</p>\n</aside>\n<p>Migrations will run the same way on the same dataset and produce consistent\nresults, meaning that what you see in development and staging is, under the\nsame circumstances, exactly what will happen in production.</p>\n<p>Django will make migrations for any change to your models or fields - even\noptions that don't affect the database - as the only way it can reconstruct\na field correctly is to have all the changes in the history, and you might\nneed those options in some data migrations later on (for example, if you've\nset custom validators).</p>\n</section>\n<section id=\"backend-support\">\n<h2>Dukungan backend<a class=\"heading-anchor\" href=\"#backend-support\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Migrations are supported on all backends that Django ships with, as well\nas any third-party backends if they have programmed in support for schema\nalteration (done via the <a class=\"reference internal\" href=\"/id/4.0/ref/schema-editor/\"><span class=\"doc\">SchemaEditor</span></a> class).</p>\n<p>However, some databases are more capable than others when it comes to\nschema migrations; some of the caveats are covered below.</p>\n<section id=\"postgresql\">\n<h3>PostgreSQL<a class=\"heading-anchor\" href=\"#postgresql\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>PostgreSQL is the most capable of all the databases here in terms of schema\nsupport.</p>\n<p>The only caveat is that prior to PostgreSQL 11, adding columns with default\nvalues causes a full rewrite of the table, for a time proportional to its size.\nFor this reason, it's recommended you always create new columns with\n<code class=\"docutils literal notranslate\"><span class=\"pre\">null=True</span></code>, as this way they will be added immediately.</p>\n</section>\n<section id=\"mysql\">\n<h3>MySQL<a class=\"heading-anchor\" href=\"#mysql\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>MySQL lacks support for transactions around schema alteration operations,\nmeaning that if a migration fails to apply you will have to manually unpick\nthe changes in order to try again (it's impossible to roll back to an\nearlier point).</p>\n<p>In addition, MySQL will fully rewrite tables for almost every schema operation\nand generally takes a time proportional to the number of rows in the table to\nadd or remove columns. On slower hardware this can be worse than a minute per\nmillion rows - adding a few columns to a table with just a few million rows\ncould lock your site up for over ten minutes.</p>\n<p>Finally, MySQL has relatively small limits on name lengths for columns, tables\nand indexes, as well as a limit on the combined size of all columns an index\ncovers. This means that indexes that are possible on other backends will\nfail to be created under MySQL.</p>\n</section>\n<section id=\"sqlite\">\n<h3>SQLite<a class=\"heading-anchor\" href=\"#sqlite\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>SQLite has very little built-in schema alteration support, and so Django\nattempts to emulate it by:</p>\n<ul class=\"simple\">\n<li><p>Membuat tabel baru dengan skema baru</p></li>\n<li><p>Menyalin data di seluruh</p></li>\n<li><p>Menghapus tabel lama</p></li>\n<li><p>Menamai kembali tabel baru untuk cocok dengan nama asli</p></li>\n</ul>\n<p>This process generally works well, but it can be slow and occasionally\nbuggy. It is not recommended that you run and migrate SQLite in a\nproduction environment unless you are very aware of the risks and\nits limitations; the support Django ships with is designed to allow\ndevelopers to use SQLite on their local machines to develop less complex\nDjango projects without the need for a full database.</p>\n</section>\n</section>\n<section id=\"workflow\">\n<h2>Alur kerja<a class=\"heading-anchor\" href=\"#workflow\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django can create migrations for you. Make changes to your models - say, add a\nfield and remove a model - and then run <a class=\"reference internal\" href=\"/id/4.0/ref/django-admin/#django-admin-makemigrations\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">makemigrations</span></code></a>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ python manage.py makemigrations\nMigrations for &#39;books&#39;:\n  books/migrations/0003_auto.py:\n    - Alter field author on book\n</code></pre></div>\n<p>Your models will be scanned and compared to the versions currently\ncontained in your migration files, and then a new set of migrations\nwill be written out. Make sure to read the output to see what\n<code class=\"docutils literal notranslate\"><span class=\"pre\">makemigrations</span></code> thinks you have changed - it's not perfect, and for\ncomplex changes it might not be detecting what you expect.</p>\n<p>Once you have your new migration files, you should apply them to your\ndatabase to make sure they work as expected:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ python manage.py migrate\nOperations to perform:\n  Apply all migrations: books\nRunning migrations:\n  Rendering model states... DONE\n  Applying books.0003_auto... OK\n</code></pre></div>\n<p>Once the migration is applied, commit the migration and the models change\nto your version control system as a single commit - that way, when other\ndevelopers (or your production servers) check out the code, they'll\nget both the changes to your models and the accompanying migration at the\nsame time.</p>\n<p>If you want to give the migration(s) a meaningful name instead of a generated\none, you can use the <a class=\"reference internal\" href=\"/id/4.0/ref/django-admin/#cmdoption-makemigrations-name\"><code class=\"xref std std-option docutils literal notranslate\"><span class=\"pre\">makemigrations</span> <span class=\"pre\">--name</span></code></a> option:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ python manage.py makemigrations --name changed_my_model your_app_label\n</code></pre></div>\n<section id=\"version-control\">\n<h3>Kendali versi<a class=\"heading-anchor\" href=\"#version-control\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Because migrations are stored in version control, you'll occasionally\ncome across situations where you and another developer have both committed\na migration to the same app at the same time, resulting in two migrations\nwith the same number.</p>\n<p>Don't worry - the numbers are just there for developers' reference, Django\njust cares that each migration has a different name. Migrations specify which\nother migrations they depend on - including earlier migrations in the same\napp - in the file, so it's possible to detect when there's two new migrations\nfor the same app that aren't ordered.</p>\n<p>When this happens, Django will prompt you and give you some options. If it\nthinks it's safe enough, it will offer to automatically linearize the two\nmigrations for you. If not, you'll have to go in and modify the migrations\nyourself - don't worry, this isn't difficult, and is explained more in\n<a class=\"reference internal\" href=\"#migration-files\"><span class=\"std std-ref\">Berkas perpindahan</span></a> below.</p>\n</section>\n</section>\n<section id=\"transactions\">\n<h2>Transaksi<a class=\"heading-anchor\" href=\"#transactions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>On databases that support DDL transactions (SQLite and PostgreSQL), all\nmigration operations will run inside a single transaction by default. In\ncontrast, if a database doesn't support DDL transactions (e.g. MySQL, Oracle)\nthen all operations will run without a transaction.</p>\n<p>You can prevent a migration from running in a transaction by setting the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">atomic</span></code> attribute to <code class=\"docutils literal notranslate\"><span class=\"pre\">False</span></code>. For example:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.db</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">migrations</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Migration</span><span class=\"p\">(</span><span class=\"n\">migrations</span><span class=\"o\">.</span><span class=\"n\">Migration</span><span class=\"p\">):</span>\n    <span class=\"n\">atomic</span> <span class=\"o\">=</span> <span class=\"kc\">False</span>\n</code></pre></div>\n<p>It's also possible to execute parts of the migration inside a transaction using\n<a class=\"reference internal\" href=\"/id/4.0/topics/db/transactions/#django.db.transaction.atomic\" title=\"django.db.transaction.atomic\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">atomic()</span></code></a> or by passing <code class=\"docutils literal notranslate\"><span class=\"pre\">atomic=True</span></code> to\n<a class=\"reference internal\" href=\"/id/4.0/ref/migration-operations/#django.db.migrations.operations.RunPython\" title=\"django.db.migrations.operations.RunPython\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RunPython</span></code></a>. See\n<a class=\"reference internal\" href=\"/id/4.0/howto/writing-migrations/#non-atomic-migrations\"><span class=\"std std-ref\">Perpindahan Non-atomic</span></a> for more details.</p>\n</section>\n<section id=\"dependencies\">\n<h2>Ketergantungan<a class=\"heading-anchor\" href=\"#dependencies\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>While migrations are per-app, the tables and relationships implied by\nyour models are too complex to be created for one app at a time. When you make\na migration that requires something else to run - for example, you add a\n<code class=\"docutils literal notranslate\"><span class=\"pre\">ForeignKey</span></code> in your <code class=\"docutils literal notranslate\"><span class=\"pre\">books</span></code> app to your <code class=\"docutils literal notranslate\"><span class=\"pre\">authors</span></code> app - the resulting\nmigration will contain a dependency on a migration in <code class=\"docutils literal notranslate\"><span class=\"pre\">authors</span></code>.</p>\n<p>This means that when you run the migrations, the <code class=\"docutils literal notranslate\"><span class=\"pre\">authors</span></code> migration runs\nfirst and creates the table the <code class=\"docutils literal notranslate\"><span class=\"pre\">ForeignKey</span></code> references, and then the migration\nthat makes the <code class=\"docutils literal notranslate\"><span class=\"pre\">ForeignKey</span></code> column runs afterward and creates the constraint.\nIf this didn't happen, the migration would try to create the <code class=\"docutils literal notranslate\"><span class=\"pre\">ForeignKey</span></code>\ncolumn without the table it's referencing existing and your database would\nthrow an error.</p>\n<p>This dependency behavior affects most migration operations where you\nrestrict to a single app. Restricting to a single app (either in\n<code class=\"docutils literal notranslate\"><span class=\"pre\">makemigrations</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">migrate</span></code>) is a best-efforts promise, and not\na guarantee; any other apps that need to be used to get dependencies correct\nwill be.</p>\n<p>Apps without migrations must not have relations (<code class=\"docutils literal notranslate\"><span class=\"pre\">ForeignKey</span></code>,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">ManyToManyField</span></code>, etc.) to apps with migrations. Sometimes it may work, but\nit's not supported.</p>\n</section>\n<section id=\"migration-files\">\n<span id=\"id1\"></span><h2>Berkas perpindahan<a class=\"heading-anchor\" href=\"#migration-files\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Migrations are stored as an on-disk format, referred to here as\n&quot;migration files&quot;. These files are actually normal Python files with an\nagreed-upon object layout, written in a declarative style.</p>\n<p>Sebuah berkas perpindahan terlihat seperti:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.db</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">migrations</span><span class=\"p\">,</span> <span class=\"n\">models</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Migration</span><span class=\"p\">(</span><span class=\"n\">migrations</span><span class=\"o\">.</span><span class=\"n\">Migration</span><span class=\"p\">):</span>\n\n    <span class=\"n\">dependencies</span> <span class=\"o\">=</span> <span class=\"p\">[(</span><span class=\"s1\">&#39;migrations&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;0001_initial&#39;</span><span class=\"p\">)]</span>\n\n    <span class=\"n\">operations</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n        <span class=\"n\">migrations</span><span class=\"o\">.</span><span class=\"n\">DeleteModel</span><span class=\"p\">(</span><span class=\"s1\">&#39;Tribble&#39;</span><span class=\"p\">),</span>\n        <span class=\"n\">migrations</span><span class=\"o\">.</span><span class=\"n\">AddField</span><span class=\"p\">(</span><span class=\"s1\">&#39;Author&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;rating&#39;</span><span class=\"p\">,</span> <span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">IntegerField</span><span class=\"p\">(</span><span class=\"n\">default</span><span class=\"o\">=</span><span class=\"mi\">0</span><span class=\"p\">)),</span>\n    <span class=\"p\">]</span>\n</code></pre></div>\n<p>What Django looks for when it loads a migration file (as a Python module) is\na subclass of <code class=\"docutils literal notranslate\"><span class=\"pre\">django.db.migrations.Migration</span></code> called <code class=\"docutils literal notranslate\"><span class=\"pre\">Migration</span></code>. It then\ninspects this object for four attributes, only two of which are used\nmost of the time:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">dependencies</span></code>, a list of migrations this one depends on.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">operations</span></code>, a list of <code class=\"docutils literal notranslate\"><span class=\"pre\">Operation</span></code> classes that define what this\nmigration does.</p></li>\n</ul>\n<p>The operations are the key; they are a set of declarative instructions which\ntell Django what schema changes need to be made. Django scans them and\nbuilds an in-memory representation of all of the schema changes to all apps,\nand uses this to generate the SQL which makes the schema changes.</p>\n<p>That in-memory structure is also used to work out what the differences are\nbetween your models and the current state of your migrations; Django runs\nthrough all the changes, in order, on an in-memory set of models to come\nup with the state of your models last time you ran <code class=\"docutils literal notranslate\"><span class=\"pre\">makemigrations</span></code>. It\nthen uses these models to compare against the ones in your <code class=\"docutils literal notranslate\"><span class=\"pre\">models.py</span></code> files\nto work out what you have changed.</p>\n<p>You should rarely, if ever, need to edit migration files by hand, but\nit's entirely possible to write them manually if you need to. Some of the\nmore complex operations are not autodetectable and are only available via\na hand-written migration, so don't be scared about editing them if you have to.</p>\n<section id=\"custom-fields\">\n<h3>Bidang penyesuaian<a class=\"heading-anchor\" href=\"#custom-fields\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Anda tidak dapat merubah argumen penempatan dalam bidang penyesuaian sudah diminta tanpa memunculkan sebuah <code class=\"docutils literal notranslate\"><span class=\"pre\">TypeError</span></code>. Perpindahan lama akan memanggil metode <code class=\"docutils literal notranslate\"><span class=\"pre\">__init__</span> <span class=\"pre\">yang</span> <span class=\"pre\">telah</span> <span class=\"pre\">dirubah</span> <span class=\"pre\">dengan</span> <span class=\"pre\">tanda</span> <span class=\"pre\">tangan</span> <span class=\"pre\">lama.</span> <span class=\"pre\">Jadi</span> <span class=\"pre\">jika</span> <span class=\"pre\">anda</span> <span class=\"pre\">butuh</span> <span class=\"pre\">argumen</span> <span class=\"pre\">baru,</span> <span class=\"pre\">harap</span> <span class=\"pre\">membuat</span> <span class=\"pre\">sebuah</span> <span class=\"pre\">argumen</span> <span class=\"pre\">kata</span> <span class=\"pre\">kunci</span> <span class=\"pre\">dan</span> <span class=\"pre\">menambahkan</span> <span class=\"pre\">sesuatu</span> <span class=\"pre\">seperti</span> <span class=\"pre\">``assert</span> <span class=\"pre\">'argument_name'</span> <span class=\"pre\">in</span> <span class=\"pre\">kwargs</span></code> dalam pembangun.</p>\n</section>\n<section id=\"model-managers\">\n<span id=\"using-managers-in-migrations\"></span><h3>Pengelola model<a class=\"heading-anchor\" href=\"#model-managers\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>You can optionally serialize managers into migrations and have them available\nin <a class=\"reference internal\" href=\"/id/4.0/ref/migration-operations/#django.db.migrations.operations.RunPython\" title=\"django.db.migrations.operations.RunPython\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RunPython</span></code></a> operations. This is done\nby defining a <code class=\"docutils literal notranslate\"><span class=\"pre\">use_in_migrations</span></code> attribute on the manager class:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">MyManager</span><span class=\"p\">(</span><span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">Manager</span><span class=\"p\">):</span>\n    <span class=\"n\">use_in_migrations</span> <span class=\"o\">=</span> <span class=\"kc\">True</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">MyModel</span><span class=\"p\">(</span><span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">Model</span><span class=\"p\">):</span>\n    <span class=\"n\">objects</span> <span class=\"o\">=</span> <span class=\"n\">MyManager</span><span class=\"p\">()</span>\n</code></pre></div>\n<p>If you are using the <a class=\"reference internal\" href=\"/id/4.0/topics/db/managers/#django.db.models.from_queryset\" title=\"django.db.models.from_queryset\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">from_queryset()</span></code></a> function to\ndynamically generate a manager class, you need to inherit from the generated\nclass to make it importable:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">MyManager</span><span class=\"p\">(</span><span class=\"n\">MyBaseManager</span><span class=\"o\">.</span><span class=\"n\">from_queryset</span><span class=\"p\">(</span><span class=\"n\">CustomQuerySet</span><span class=\"p\">)):</span>\n    <span class=\"n\">use_in_migrations</span> <span class=\"o\">=</span> <span class=\"kc\">True</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">MyModel</span><span class=\"p\">(</span><span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">Model</span><span class=\"p\">):</span>\n    <span class=\"n\">objects</span> <span class=\"o\">=</span> <span class=\"n\">MyManager</span><span class=\"p\">()</span>\n</code></pre></div>\n<p>Please refer to the notes about <a class=\"reference internal\" href=\"#historical-models\"><span class=\"std std-ref\">Historical models</span></a> in migrations to see\nthe implications that come along.</p>\n</section>\n<section id=\"initial-migrations\">\n<h3>Perpindahan awal<a class=\"heading-anchor\" href=\"#initial-migrations\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<dl class=\"py attribute\">\n<dt class=\"sig sig-object py\" id=\"django.db.migrations.Migration.initial\">\n<span class=\"sig-prename descclassname\"><span class=\"pre\">Migration.</span></span><span class=\"sig-name descname\"><span class=\"pre\">initial</span></span><a class=\"heading-anchor\" href=\"#django.db.migrations.Migration.initial\"><span class=\"visually-hidden\">Link to this definition</span><span aria-hidden=\"true\">#</span></a></dt>\n<dd></dd></dl>\n\n<p>The &quot;initial migrations&quot; for an app are the migrations that create the first\nversion of that app's tables. Usually an app will have one initial migration,\nbut in some cases of complex model interdependencies it may have two or more.</p>\n<p>Initial migrations are marked with an <code class=\"docutils literal notranslate\"><span class=\"pre\">initial</span> <span class=\"pre\">=</span> <span class=\"pre\">True</span></code> class attribute on the\nmigration class. If an <code class=\"docutils literal notranslate\"><span class=\"pre\">initial</span></code> class attribute isn't found, a migration\nwill be considered &quot;initial&quot; if it is the first migration in the app (i.e. if\nit has no dependencies on any other migration in the same app).</p>\n<p>Ketika pilihan <a class=\"reference internal\" href=\"/id/4.0/ref/django-admin/#cmdoption-migrate-fake-initial\"><code class=\"xref std std-option docutils literal notranslate\"><span class=\"pre\">migrate</span> <span class=\"pre\">--fake-initial</span></code></a> digunakan, perpindahan inisial ini diperlakukan khusus. Untuk sebuah perpindahan inisial yang membuat satu atau lebih tabel (tindakan <code class=\"docutils literal notranslate\"><span class=\"pre\">CreateModel</span></code>), Django memeriksa bahwa semua dari tabel-table tersebut sudah ada dalam basisdata dan pemberlakukan-palsu perpindahan jika demikian. Demikian pula, untuk sebuah perpindahan inisial yang menambahkan satu atau lebih bidang-bidang (tindakan <code class=\"docutils literal notranslate\"><span class=\"pre\">AddField</span></code>), Django memeriksa bahwa semua dari masing-masing kolom sudah ada dalam basisdata dan pemberlakukan-palsu perpindahan jika demikian. Tanpa <code class=\"docutils literal notranslate\"><span class=\"pre\">--fake-initial</span></code>, perpindahan inisial diperlakukan tidak berbeda dari perpindahan lain.</p>\n</section>\n<section id=\"history-consistency\">\n<span id=\"migration-history-consistency\"></span><h3>History consistency<a class=\"heading-anchor\" href=\"#history-consistency\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Seperti sebelumnya diobrolkan, anda mungkin butuh melinerarkan perpindahan secara manual ketika dua cabang pengembangan digabungkan. Selagi menyunting ketergantungan perpindahan, anda dapat secara tidak sengaja membuat sebuah keadaan riwayat tidak tetap dimana sebuah perpindahan telah diberlakukan tetapi beberapa ketergantungannya belum. Ini adalah indikasi kuat yang ketergantungan adalah tidak benar, jadi Django akan menolak menjalankan perpindahan atau membuat perpindahan baru sampai itu diperbaiki. Ketika menggunakan banyak basisdata, anda dapat menggunakan metode <a class=\"reference internal\" href=\"/id/4.0/topics/db/multi-db/#allow_migrate\" title=\"allow_migrate\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">allow_migrate()</span></code></a> dari <a class=\"reference internal\" href=\"/id/4.0/topics/db/multi-db/#topics-db-multi-db-routing\"><span class=\"std std-ref\">database routers</span></a> untuk mengendalikan pemeriksaan <a class=\"reference internal\" href=\"/id/4.0/ref/django-admin/#django-admin-makemigrations\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">makemigrations</span></code></a> basisdata mana untuk riwayat ketetapan.</p>\n</section>\n</section>\n<section id=\"adding-migrations-to-apps\">\n<h2>Menambahkan perpindahan ke aplikasi<a class=\"heading-anchor\" href=\"#adding-migrations-to-apps\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>New apps come preconfigured to accept migrations, and so you can add migrations\nby running <a class=\"reference internal\" href=\"/id/4.0/ref/django-admin/#django-admin-makemigrations\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">makemigrations</span></code></a> once you've made some changes.</p>\n<p>If your app already has models and database tables, and doesn't have migrations\nyet (for example, you created it against a previous Django version), you'll\nneed to convert it to use migrations by running:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ python manage.py makemigrations your_app_label\n</code></pre></div>\n<p>This will make a new initial migration for your app. Now, run <code class=\"docutils literal notranslate\"><span class=\"pre\">python</span>\n<span class=\"pre\">manage.py</span> <span class=\"pre\">migrate</span> <span class=\"pre\">--fake-initial</span></code>, and Django will detect that you have an\ninitial migration <em>and</em> that the tables it wants to create already exist, and\nwill mark the migration as already applied. (Without the <a class=\"reference internal\" href=\"/id/4.0/ref/django-admin/#cmdoption-migrate-fake-initial\"><code class=\"xref std std-option docutils literal notranslate\"><span class=\"pre\">migrate</span>\n<span class=\"pre\">--fake-initial</span></code></a> flag, the command would error out because the tables it wants\nto create already exist.)</p>\n<p>Catat bahwa ini hanya bekerja memberikan dua hal:</p>\n<ul class=\"simple\">\n<li><p>You have not changed your models since you made their tables. For migrations\nto work, you must make the initial migration <em>first</em> and then make changes,\nas Django compares changes against migration files, not the database.</p></li>\n<li><p>You have not manually edited your database - Django won't be able to detect\nthat your database doesn't match your models, you'll just get errors when\nmigrations try to modify those tables.</p></li>\n</ul>\n</section>\n<section id=\"reversing-migrations\">\n<span id=\"id2\"></span><h2>Reversing migrations<a class=\"heading-anchor\" href=\"#reversing-migrations\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Migrations can be reversed with <a class=\"reference internal\" href=\"/id/4.0/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">migrate</span></code></a> by passing the number of the\nprevious migration. For example, to reverse migration <code class=\"docutils literal notranslate\"><span class=\"pre\">books.0003</span></code>:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-0-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>python<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>migrate<span class=\"w\"> </span>books<span class=\"w\"> </span><span class=\"m\">0002</span>\n<span class=\"go\">Operations to perform:</span>\n<span class=\"go\">  Target specific migration: 0002_auto, from books</span>\n<span class=\"go\">Running migrations:</span>\n<span class=\"go\">  Rendering model states... DONE</span>\n<span class=\"go\">  Unapplying books.0003_auto... OK</span>\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-0-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> py manage.py migrate books 0002\n<span class=\"go\">Operations to perform:</span>\n<span class=\"go\">  Target specific migration: 0002_auto, from books</span>\n<span class=\"go\">Running migrations:</span>\n<span class=\"go\">  Rendering model states... DONE</span>\n<span class=\"go\">  Unapplying books.0003_auto... OK</span>\n</code></pre></div></div></div>\n<p>If you want to reverse all migrations applied for an app, use the name\n<code class=\"docutils literal notranslate\"><span class=\"pre\">zero</span></code>:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-1-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>python<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>migrate<span class=\"w\"> </span>books<span class=\"w\"> </span>zero\n<span class=\"go\">Operations to perform:</span>\n<span class=\"go\">  Unapply all migrations: books</span>\n<span class=\"go\">Running migrations:</span>\n<span class=\"go\">  Rendering model states... DONE</span>\n<span class=\"go\">  Unapplying books.0002_auto... OK</span>\n<span class=\"go\">  Unapplying books.0001_initial... OK</span>\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-1-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> py manage.py migrate books zero\n<span class=\"go\">Operations to perform:</span>\n<span class=\"go\">  Unapply all migrations: books</span>\n<span class=\"go\">Running migrations:</span>\n<span class=\"go\">  Rendering model states... DONE</span>\n<span class=\"go\">  Unapplying books.0002_auto... OK</span>\n<span class=\"go\">  Unapplying books.0001_initial... OK</span>\n</code></pre></div></div></div>\n<p>A migration is irreversible if it contains any irreversible operations.\nAttempting to reverse such migrations will raise <code class=\"docutils literal notranslate\"><span class=\"pre\">IrreversibleError</span></code>:</p>\n<div class=\"console\" data-console><div class=\"console-panel\" data-platform=\"unix\"><p class=\"console-label\" id=\"console-2-unix-label\">Linux / macOS</p><div class=\"code-block\" data-language=\"console\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Shell</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Shell code\"><code><span class=\"gp\">$ </span>python<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>migrate<span class=\"w\"> </span>books<span class=\"w\"> </span><span class=\"m\">0002</span>\n<span class=\"go\">Operations to perform:</span>\n<span class=\"go\">  Target specific migration: 0002_auto, from books</span>\n<span class=\"go\">Running migrations:</span>\n<span class=\"go\">  Rendering model states... DONE</span>\n<span class=\"go\">  Unapplying books.0003_auto...Traceback (most recent call last):</span>\n<span class=\"go\">django.db.migrations.exceptions.IrreversibleError: Operation &lt;RunSQL  sql=&#39;DROP TABLE demo_books&#39;&gt; in books.0003_auto is not reversible</span>\n</code></pre></div>\n</div><div class=\"console-panel\" data-platform=\"windows\"><p class=\"console-label\" id=\"console-2-windows-label\">Windows</p><div class=\"code-block\" data-language=\"doscon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Windows</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Windows shell\"><code><span class=\"gp\">...\\&gt;</span> py manage.py migrate books 0002\n<span class=\"go\">Operations to perform:</span>\n<span class=\"go\">  Target specific migration: 0002_auto, from books</span>\n<span class=\"go\">Running migrations:</span>\n<span class=\"go\">  Rendering model states... DONE</span>\n<span class=\"go\">  Unapplying books.0003_auto...Traceback (most recent call last):</span>\n<span class=\"gp\">django.db.migrations.exceptions.IrreversibleError: Operation &lt;RunSQL  sql=&#39;DROP TABLE demo_books&#39;&gt;</span> in books.0003_auto is not reversible\n</code></pre></div></div></div>\n</section>\n<section id=\"historical-models\">\n<span id=\"id3\"></span><h2>Historical models<a class=\"heading-anchor\" href=\"#historical-models\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>When you run migrations, Django is working from historical versions of your\nmodels stored in the migration files. If you write Python code using the\n<a class=\"reference internal\" href=\"/id/4.0/ref/migration-operations/#django.db.migrations.operations.RunPython\" title=\"django.db.migrations.operations.RunPython\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RunPython</span></code></a> operation, or if you have\n<code class=\"docutils literal notranslate\"><span class=\"pre\">allow_migrate</span></code> methods on your database routers, you <strong>need to use</strong> these\nhistorical model versions rather than importing them directly.</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Peringatan</p>\n<p>If you import models directly rather than using the historical models,\nyour migrations <em>may work initially</em> but will fail in the future when you\ntry to re-run old migrations (commonly, when you set up a new installation\nand run through all the migrations to set up the database).</p>\n<p>This means that historical model problems may not be immediately obvious.\nIf you run into this kind of failure, it's OK to edit the migration to use\nthe historical models rather than direct imports and commit those changes.</p>\n</aside>\n<p>Karena itu tidak mungkin menserialisasikan kode Python berubah-ubah, model-model riwayat ini tidak akan mempunyai penyesuaian metode apapun yang anda telah tentukan. Mereka akan, bagaimanapun, mempunyai bidang-bidang sama, hubungan, pengelola (terbatas ke itu dengan <code class=\"docutils literal notranslate\"><span class=\"pre\">use_in_migrations</span> <span class=\"pre\">=</span> <span class=\"pre\">True</span></code>) dan pilihan <code class=\"docutils literal notranslate\"><span class=\"pre\">Meta</span></code> (juga berversi, jadi mereka mungkin berbeda dari satu anda saat ini).</p>\n<aside class=\"admonition admonition-warning\" role=\"note\">\n<p class=\"admonition-title\">Peringatan</p>\n<p>Ini berarti bahwa anda TIDAK akan mempunyai metode <code class=\"docutils literal notranslate\"><span class=\"pre\">save()</span></code> penyesuaian dipanggil ketika anda mengakses mereka dalam perpindahan, dan anda TIDAK akan mempunyai pembangun penyesuaian apapun atau metode instance. Rencanakan dengan tepat!</p>\n</aside>\n<p>References to functions in field options such as <code class=\"docutils literal notranslate\"><span class=\"pre\">upload_to</span></code> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">limit_choices_to</span></code> and model manager declarations with managers having\n<code class=\"docutils literal notranslate\"><span class=\"pre\">use_in_migrations</span> <span class=\"pre\">=</span> <span class=\"pre\">True</span></code> are serialized in migrations, so the functions and\nclasses will need to be kept around for as long as there is a migration\nreferencing them. Any <a class=\"reference internal\" href=\"/id/4.0/howto/custom-model-fields/\"><span class=\"doc\">custom model fields</span></a>\nwill also need to be kept, since these are imported directly by migrations.</p>\n<p>In addition, the concrete base classes of the model are stored as pointers, so\nyou must always keep base classes around for as long as there is a migration\nthat contains a reference to them. On the plus side, methods and managers from\nthese base classes inherit normally, so if you absolutely need access to these\nyou can opt to move them into a superclass.</p>\n<p>Untuk memindahkan acuan lama, anda dapat <a class=\"reference internal\" href=\"#migration-squashing\"><span class=\"std std-ref\">squash migrations</span></a> atau, jika tidak ada acuan, salin mereka kedalam berkas perpindahan.</p>\n</section>\n<section id=\"considerations-when-removing-model-fields\">\n<span id=\"migrations-removing-model-fields\"></span><h2>Pertimbangkan ketika memindahkan bidang model<a class=\"heading-anchor\" href=\"#considerations-when-removing-model-fields\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Similar to the &quot;references to historical functions&quot; considerations described in\nthe previous section, removing custom model fields from your project or\nthird-party app will cause a problem if they are referenced in old migrations.</p>\n<p>Untuk membantu dengan keadaan ini, Django menyediakan beberapa atribut bidang model untuk memandu dengan pengusangan bidang model menggunakan <a class=\"reference internal\" href=\"/id/4.0/topics/checks/\"><span class=\"doc\">system checks framework</span></a>.</p>\n<p>Tambah atribut <code class=\"docutils literal notranslate\"><span class=\"pre\">system_check_deprecated_details</span></code> ke bidang model anda mirip seperti berikut:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">IPAddressField</span><span class=\"p\">(</span><span class=\"n\">Field</span><span class=\"p\">):</span>\n    <span class=\"n\">system_check_deprecated_details</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n        <span class=\"s1\">&#39;msg&#39;</span><span class=\"p\">:</span> <span class=\"p\">(</span>\n            <span class=\"s1\">&#39;IPAddressField has been deprecated. Support for it (except &#39;</span>\n            <span class=\"s1\">&#39;in historical migrations) will be removed in Django 1.9.&#39;</span>\n        <span class=\"p\">),</span>\n        <span class=\"s1\">&#39;hint&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;Use GenericIPAddressField instead.&#39;</span><span class=\"p\">,</span>  <span class=\"c1\"># optional</span>\n        <span class=\"s1\">&#39;id&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;fields.W900&#39;</span><span class=\"p\">,</span>  <span class=\"c1\"># pick a unique ID for your field.</span>\n    <span class=\"p\">}</span>\n</code></pre></div>\n<p>After a deprecation period of your choosing (two or three feature releases for\nfields in Django itself), change the <code class=\"docutils literal notranslate\"><span class=\"pre\">system_check_deprecated_details</span></code>\nattribute to <code class=\"docutils literal notranslate\"><span class=\"pre\">system_check_removed_details</span></code> and update the dictionary similar\nto:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">IPAddressField</span><span class=\"p\">(</span><span class=\"n\">Field</span><span class=\"p\">):</span>\n    <span class=\"n\">system_check_removed_details</span> <span class=\"o\">=</span> <span class=\"p\">{</span>\n        <span class=\"s1\">&#39;msg&#39;</span><span class=\"p\">:</span> <span class=\"p\">(</span>\n            <span class=\"s1\">&#39;IPAddressField has been removed except for support in &#39;</span>\n            <span class=\"s1\">&#39;historical migrations.&#39;</span>\n        <span class=\"p\">),</span>\n        <span class=\"s1\">&#39;hint&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;Use GenericIPAddressField instead.&#39;</span><span class=\"p\">,</span>\n        <span class=\"s1\">&#39;id&#39;</span><span class=\"p\">:</span> <span class=\"s1\">&#39;fields.E900&#39;</span><span class=\"p\">,</span>  <span class=\"c1\"># pick a unique ID for your field.</span>\n    <span class=\"p\">}</span>\n</code></pre></div>\n<p>You should keep the field's methods that are required for it to operate in\ndatabase migrations such as <code class=\"docutils literal notranslate\"><span class=\"pre\">__init__()</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">deconstruct()</span></code>, and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">get_internal_type()</span></code>. Keep this stub field for as long as any migrations\nwhich reference the field exist. For example, after squashing migrations and\nremoving the old ones, you should be able to remove the field completely.</p>\n</section>\n<section id=\"data-migrations\">\n<span id=\"id4\"></span><h2>Perpindahan Data<a class=\"heading-anchor\" href=\"#data-migrations\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>As well as changing the database schema, you can also use migrations to change\nthe data in the database itself, in conjunction with the schema if you want.</p>\n<p>Migrations that alter data are usually called &quot;data migrations&quot;; they're best\nwritten as separate migrations, sitting alongside your schema migrations.</p>\n<p>Django can't automatically generate data migrations for you, as it does with\nschema migrations, but it's not very hard to write them. Migration files in\nDjango are made up of <a class=\"reference internal\" href=\"/id/4.0/ref/migration-operations/\"><span class=\"doc\">Operations</span></a>, and\nthe main operation you use for data migrations is\n<a class=\"reference internal\" href=\"/id/4.0/ref/migration-operations/#django.db.migrations.operations.RunPython\" title=\"django.db.migrations.operations.RunPython\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RunPython</span></code></a>.</p>\n<p>To start, make an empty migration file you can work from (Django will put\nthe file in the right place, suggest a name, and add dependencies for you):</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"n\">python</span> <span class=\"n\">manage</span><span class=\"o\">.</span><span class=\"n\">py</span> <span class=\"n\">makemigrations</span> <span class=\"o\">--</span><span class=\"n\">empty</span> <span class=\"n\">yourappname</span>\n</code></pre></div>\n<p>Lalu, buka berkas; dia seharusnya kelihatan sesuatu sepert ini:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"c1\"># Generated by Django A.B on YYYY-MM-DD HH:MM</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.db</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">migrations</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Migration</span><span class=\"p\">(</span><span class=\"n\">migrations</span><span class=\"o\">.</span><span class=\"n\">Migration</span><span class=\"p\">):</span>\n\n    <span class=\"n\">dependencies</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n        <span class=\"p\">(</span><span class=\"s1\">&#39;yourappname&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;0001_initial&#39;</span><span class=\"p\">),</span>\n    <span class=\"p\">]</span>\n\n    <span class=\"n\">operations</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"p\">]</span>\n</code></pre></div>\n<p>Sekarang, semua yang perlu anda lakukan adalah membuat sebuah fungsi baru dan memiliki <a class=\"reference internal\" href=\"/id/4.0/ref/migration-operations/#django.db.migrations.operations.RunPython\" title=\"django.db.migrations.operations.RunPython\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RunPython</span></code></a> menggunakannya. <a class=\"reference internal\" href=\"/id/4.0/ref/migration-operations/#django.db.migrations.operations.RunPython\" title=\"django.db.migrations.operations.RunPython\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RunPython</span></code></a> mengharapkan sebuah callable sebagai argumennya yang mengambil dua argumen - pertama adalah sebuah <a class=\"reference internal\" href=\"/id/4.0/ref/applications/\"><span class=\"doc\">app registry</span></a> yang mempunyai versi riwayat dari semua model anda dimuat kedalam itu untuk mencocokkan dimana riwayat perpindahan anda berada, dan kedua adalah <a class=\"reference internal\" href=\"/id/4.0/ref/schema-editor/\"><span class=\"doc\">SchemaEditor</span></a>, yang dapat menggunakan secara manual mempengaruhi perubahan skema basisdata (tetapi hati-hati, melakukan ini dapat membingungkan pengenal otomatis perpindahan!)</p>\n<p>Let's write a migration that populates our new <code class=\"docutils literal notranslate\"><span class=\"pre\">name</span></code> field with the combined\nvalues of <code class=\"docutils literal notranslate\"><span class=\"pre\">first_name</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">last_name</span></code> (we've come to our senses and\nrealized that not everyone has first and last names). All we need to do is use\nthe historical model and iterate over the rows:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.db</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">migrations</span>\n\n<span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">combine_names</span><span class=\"p\">(</span><span class=\"n\">apps</span><span class=\"p\">,</span> <span class=\"n\">schema_editor</span><span class=\"p\">):</span>\n    <span class=\"c1\"># We can&#39;t import the Person model directly as it may be a newer</span>\n    <span class=\"c1\"># version than this migration expects. We use the historical version.</span>\n    <span class=\"n\">Person</span> <span class=\"o\">=</span> <span class=\"n\">apps</span><span class=\"o\">.</span><span class=\"n\">get_model</span><span class=\"p\">(</span><span class=\"s1\">&#39;yourappname&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;Person&#39;</span><span class=\"p\">)</span>\n    <span class=\"k\">for</span> <span class=\"n\">person</span> <span class=\"ow\">in</span> <span class=\"n\">Person</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">all</span><span class=\"p\">():</span>\n        <span class=\"n\">person</span><span class=\"o\">.</span><span class=\"n\">name</span> <span class=\"o\">=</span> <span class=\"s1\">&#39;</span><span class=\"si\">%s</span><span class=\"s1\"> </span><span class=\"si\">%s</span><span class=\"s1\">&#39;</span> <span class=\"o\">%</span> <span class=\"p\">(</span><span class=\"n\">person</span><span class=\"o\">.</span><span class=\"n\">first_name</span><span class=\"p\">,</span> <span class=\"n\">person</span><span class=\"o\">.</span><span class=\"n\">last_name</span><span class=\"p\">)</span>\n        <span class=\"n\">person</span><span class=\"o\">.</span><span class=\"n\">save</span><span class=\"p\">()</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Migration</span><span class=\"p\">(</span><span class=\"n\">migrations</span><span class=\"o\">.</span><span class=\"n\">Migration</span><span class=\"p\">):</span>\n\n    <span class=\"n\">dependencies</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n        <span class=\"p\">(</span><span class=\"s1\">&#39;yourappname&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;0001_initial&#39;</span><span class=\"p\">),</span>\n    <span class=\"p\">]</span>\n\n    <span class=\"n\">operations</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n        <span class=\"n\">migrations</span><span class=\"o\">.</span><span class=\"n\">RunPython</span><span class=\"p\">(</span><span class=\"n\">combine_names</span><span class=\"p\">),</span>\n    <span class=\"p\">]</span>\n</code></pre></div>\n<p>Once that's done, we can run <code class=\"docutils literal notranslate\"><span class=\"pre\">python</span> <span class=\"pre\">manage.py</span> <span class=\"pre\">migrate</span></code> as normal and the\ndata migration will run in place alongside other migrations.</p>\n<p>You can pass a second callable to\n<a class=\"reference internal\" href=\"/id/4.0/ref/migration-operations/#django.db.migrations.operations.RunPython\" title=\"django.db.migrations.operations.RunPython\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RunPython</span></code></a> to run whatever logic you\nwant executed when migrating backwards. If this callable is omitted, migrating\nbackwards will raise an exception.</p>\n<section id=\"accessing-models-from-other-apps\">\n<h3>Mengakses model dari aplikasi lain<a class=\"heading-anchor\" href=\"#accessing-models-from-other-apps\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>When writing a <code class=\"docutils literal notranslate\"><span class=\"pre\">RunPython</span></code> function that uses models from apps other than the\none in which the migration is located, the migration's <code class=\"docutils literal notranslate\"><span class=\"pre\">dependencies</span></code>\nattribute should include the latest migration of each app that is involved,\notherwise you may get an error similar to: <code class=\"docutils literal notranslate\"><span class=\"pre\">LookupError:</span> <span class=\"pre\">No</span> <span class=\"pre\">installed</span> <span class=\"pre\">app</span>\n<span class=\"pre\">with</span> <span class=\"pre\">label</span> <span class=\"pre\">'myappname'</span></code> when you try to retrieve the model in the <code class=\"docutils literal notranslate\"><span class=\"pre\">RunPython</span></code>\nfunction using <code class=\"docutils literal notranslate\"><span class=\"pre\">apps.get_model()</span></code>.</p>\n<p>In the following example, we have a migration in <code class=\"docutils literal notranslate\"><span class=\"pre\">app1</span></code> which needs to use\nmodels in <code class=\"docutils literal notranslate\"><span class=\"pre\">app2</span></code>. We aren't concerned with the details of <code class=\"docutils literal notranslate\"><span class=\"pre\">move_m1</span></code> other\nthan the fact it will need to access models from both apps. Therefore we've\nadded a dependency that specifies the last migration of <code class=\"docutils literal notranslate\"><span class=\"pre\">app2</span></code>:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Migration</span><span class=\"p\">(</span><span class=\"n\">migrations</span><span class=\"o\">.</span><span class=\"n\">Migration</span><span class=\"p\">):</span>\n\n    <span class=\"n\">dependencies</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n        <span class=\"p\">(</span><span class=\"s1\">&#39;app1&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;0001_initial&#39;</span><span class=\"p\">),</span>\n        <span class=\"c1\"># added dependency to enable using models from app2 in move_m1</span>\n        <span class=\"p\">(</span><span class=\"s1\">&#39;app2&#39;</span><span class=\"p\">,</span> <span class=\"s1\">&#39;0004_foobar&#39;</span><span class=\"p\">),</span>\n    <span class=\"p\">]</span>\n\n    <span class=\"n\">operations</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n        <span class=\"n\">migrations</span><span class=\"o\">.</span><span class=\"n\">RunPython</span><span class=\"p\">(</span><span class=\"n\">move_m1</span><span class=\"p\">),</span>\n    <span class=\"p\">]</span>\n</code></pre></div>\n</section>\n<section id=\"more-advanced-migrations\">\n<h3>Perpindahan lebih lanjut<a class=\"heading-anchor\" href=\"#more-advanced-migrations\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>If you're interested in the more advanced migration operations, or want\nto be able to write your own, see the <a class=\"reference internal\" href=\"/id/4.0/ref/migration-operations/\"><span class=\"doc\">migration operations reference</span></a> and the &quot;how-to&quot; on <a class=\"reference internal\" href=\"/id/4.0/howto/writing-migrations/\"><span class=\"doc\">writing migrations</span></a>.</p>\n</section>\n</section>\n<section id=\"squashing-migrations\">\n<span id=\"migration-squashing\"></span><h2>Menginjak perpindahan<a class=\"heading-anchor\" href=\"#squashing-migrations\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>You are encouraged to make migrations freely and not worry about how many you\nhave; the migration code is optimized to deal with hundreds at a time without\nmuch slowdown. However, eventually you will want to move back from having\nseveral hundred migrations to just a few, and that's where squashing comes in.</p>\n<p>Squashing is the act of reducing an existing set of many migrations down to\none (or sometimes a few) migrations which still represent the same changes.</p>\n<p>Django does this by taking all of your existing migrations, extracting their\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Operation</span></code>s and putting them all in sequence, and then running an optimizer\nover them to try and reduce the length of the list - for example, it knows\nthat <a class=\"reference internal\" href=\"/id/4.0/ref/migration-operations/#django.db.migrations.operations.CreateModel\" title=\"django.db.migrations.operations.CreateModel\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CreateModel</span></code></a> and\n<a class=\"reference internal\" href=\"/id/4.0/ref/migration-operations/#django.db.migrations.operations.DeleteModel\" title=\"django.db.migrations.operations.DeleteModel\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">DeleteModel</span></code></a> cancel each other out,\nand it knows that <a class=\"reference internal\" href=\"/id/4.0/ref/migration-operations/#django.db.migrations.operations.AddField\" title=\"django.db.migrations.operations.AddField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">AddField</span></code></a> can be\nrolled into <a class=\"reference internal\" href=\"/id/4.0/ref/migration-operations/#django.db.migrations.operations.CreateModel\" title=\"django.db.migrations.operations.CreateModel\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CreateModel</span></code></a>.</p>\n<p>Once the operation sequence has been reduced as much as possible - the amount\npossible depends on how closely intertwined your models are and if you have\nany <a class=\"reference internal\" href=\"/id/4.0/ref/migration-operations/#django.db.migrations.operations.RunSQL\" title=\"django.db.migrations.operations.RunSQL\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RunSQL</span></code></a>\nor <a class=\"reference internal\" href=\"/id/4.0/ref/migration-operations/#django.db.migrations.operations.RunPython\" title=\"django.db.migrations.operations.RunPython\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">RunPython</span></code></a> operations (which can't\nbe optimized through unless they are marked as <code class=\"docutils literal notranslate\"><span class=\"pre\">elidable</span></code>) - Django will then\nwrite it back out into a new set of migration files.</p>\n<p>These files are marked to say they replace the previously-squashed migrations,\nso they can coexist with the old migration files, and Django will intelligently\nswitch between them depending where you are in the history. If you're still\npart-way through the set of migrations that you squashed, it will keep using\nthem until it hits the end and then switch to the squashed history, while new\ninstalls will use the new squashed migration and skip all the old ones.</p>\n<p>This enables you to squash and not mess up systems currently in production\nthat aren't fully up-to-date yet. The recommended process is to squash, keeping\nthe old files, commit and release, wait until all systems are upgraded with\nthe new release (or if you're a third-party project, ensure your users upgrade\nreleases in order without skipping any), and then remove the old files, commit\nand do a second release.</p>\n<p>The command that backs all this is <a class=\"reference internal\" href=\"/id/4.0/ref/django-admin/#django-admin-squashmigrations\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">squashmigrations</span></code></a> - pass it the\napp label and migration name you want to squash up to, and it'll get to work:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code>$ ./manage.py squashmigrations myapp 0004\nWill squash the following migrations:\n - 0001_initial\n - 0002_some_change\n - 0003_another_change\n - 0004_undo_something\nDo you wish to proceed? [yN] y\nOptimizing...\n  Optimized from 12 operations to 7 operations.\nCreated new squashed migration /home/andrew/Programs/DjangoTest/test/migrations/0001_squashed_0004_undo_something.py\n  You should commit this migration but leave the old ones in place;\n  the new migration will be used for new installs. Once you are sure\n  all instances of the codebase have applied the migrations you squashed,\n  you can delete them.\n</code></pre></div>\n<p>Use the <a class=\"reference internal\" href=\"/id/4.0/ref/django-admin/#cmdoption-squashmigrations-squashed-name\"><code class=\"xref std std-option docutils literal notranslate\"><span class=\"pre\">squashmigrations</span> <span class=\"pre\">--squashed-name</span></code></a> option if you want to set\nthe name of the squashed migration rather than use an autogenerated one.</p>\n<p>Note that model interdependencies in Django can get very complex, and squashing\nmay result in migrations that do not run; either mis-optimized (in which case\nyou can try again with <code class=\"docutils literal notranslate\"><span class=\"pre\">--no-optimize</span></code>, though you should also report an issue),\nor with a <code class=\"docutils literal notranslate\"><span class=\"pre\">CircularDependencyError</span></code>, in which case you can manually resolve it.</p>\n<p>To manually resolve a <code class=\"docutils literal notranslate\"><span class=\"pre\">CircularDependencyError</span></code>, break out one of\nthe ForeignKeys in the circular dependency loop into a separate\nmigration, and move the dependency on the other app with it. If you're unsure,\nsee how <a class=\"reference internal\" href=\"/id/4.0/ref/django-admin/#django-admin-makemigrations\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">makemigrations</span></code></a> deals with the problem when asked to create\nbrand new migrations from your models. In a future release of Django,\n<a class=\"reference internal\" href=\"/id/4.0/ref/django-admin/#django-admin-squashmigrations\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">squashmigrations</span></code></a> will be updated to attempt to resolve these errors\nitself.</p>\n<p>Once you've squashed your migration, you should then commit it alongside the\nmigrations it replaces and distribute this change to all running instances\nof your application, making sure that they run <code class=\"docutils literal notranslate\"><span class=\"pre\">migrate</span></code> to store the change\nin their database.</p>\n<p>You must then transition the squashed migration to a normal migration by:</p>\n<ul class=\"simple\">\n<li><p>Menghapus semua berkas perpindahan yang diganti</p></li>\n<li><p>Updating all migrations that depend on the deleted migrations to depend on\nthe squashed migration instead.</p></li>\n<li><p>Removing the <code class=\"docutils literal notranslate\"><span class=\"pre\">replaces</span></code> attribute in the <code class=\"docutils literal notranslate\"><span class=\"pre\">Migration</span></code> class of the\nsquashed migration (this is how Django tells that it is a squashed migration).</p></li>\n</ul>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Catatan</p>\n<p>Once you've squashed a migration, you should not then re-squash that squashed\nmigration until you have fully transitioned it to a normal migration.</p>\n</aside>\n</section>\n<section id=\"serializing-values\">\n<span id=\"migration-serializing\"></span><h2>Serializing values<a class=\"heading-anchor\" href=\"#serializing-values\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Migrations are Python files containing the old definitions of your models\n- thus, to write them, Django must take the current state of your models and\nserialize them out into a file.</p>\n<p>While Django can serialize most things, there are some things that we just\ncan't serialize out into a valid Python representation - there's no Python\nstandard for how a value can be turned back into code (<code class=\"docutils literal notranslate\"><span class=\"pre\">repr()</span></code> only works\nfor basic values, and doesn't specify import paths).</p>\n<p>Django can serialize the following:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">int</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">float</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">bool</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">str</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">bytes</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">None</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">NoneType</span></code></p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">list</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">set</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">tuple</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">dict</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">range</span></code>.</p></li>\n<li><p>Instance <code class=\"docutils literal notranslate\"><span class=\"pre\">datetime.date</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">datetime.time</span></code>, dan <code class=\"docutils literal notranslate\"><span class=\"pre\">datetime.datetime</span></code> (termasuk itu yang menyadari zona waktu)</p></li>\n<li><p>Instance <code class=\"docutils literal notranslate\"><span class=\"pre\">decimal.Decimal</span></code></p></li>\n<li><p>Instance <code class=\"docutils literal notranslate\"><span class=\"pre\">enum.Enum</span></code></p></li>\n<li><p>Instance <code class=\"docutils literal notranslate\"><span class=\"pre\">uuid.UUID</span></code></p></li>\n<li><p><a class=\"reference external\" href=\"https://docs.python.org/3/library/functools.html#functools.partial\" title=\"(di Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">functools.partial()</span></code></a> and <a class=\"reference external\" href=\"https://docs.python.org/3/library/functools.html#functools.partialmethod\" title=\"(di Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">functools.partialmethod</span></code></a> instances\nwhich have serializable <code class=\"docutils literal notranslate\"><span class=\"pre\">func</span></code>, <code class=\"docutils literal notranslate\"><span class=\"pre\">args</span></code>, and <code class=\"docutils literal notranslate\"><span class=\"pre\">keywords</span></code> values.</p></li>\n<li><p>Pure and concrete path objects from <a class=\"reference external\" href=\"https://docs.python.org/3/library/pathlib.html#module-pathlib\" title=\"(di Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">pathlib</span></code></a>. Concrete paths are\nconverted to their pure path equivalent, e.g. <a class=\"reference external\" href=\"https://docs.python.org/3/library/pathlib.html#pathlib.PosixPath\" title=\"(di Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">pathlib.PosixPath</span></code></a> to\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/pathlib.html#pathlib.PurePosixPath\" title=\"(di Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">pathlib.PurePosixPath</span></code></a>.</p></li>\n<li><p><a class=\"reference external\" href=\"https://docs.python.org/3/library/os.html#os.PathLike\" title=\"(di Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">os.PathLike</span></code></a> instances, e.g. <a class=\"reference external\" href=\"https://docs.python.org/3/library/os.html#os.DirEntry\" title=\"(di Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">os.DirEntry</span></code></a>, which are\nconverted to <code class=\"docutils literal notranslate\"><span class=\"pre\">str</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">bytes</span></code> using <a class=\"reference external\" href=\"https://docs.python.org/3/library/os.html#os.fspath\" title=\"(di Python v3.14)\"><code class=\"xref py py-func docutils literal notranslate\"><span class=\"pre\">os.fspath()</span></code></a>.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">LazyObject</span></code> instances which wrap a serializable value.</p></li>\n<li><p>Enumeration types (e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">TextChoices</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">IntegerChoices</span></code>) instances.</p></li>\n<li><p>Bidang Django Apapun</p></li>\n<li><p>Any function or method reference (e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">datetime.datetime.today</span></code>) (must be in module's top-level scope)</p></li>\n<li><p>Unbound methods used from within the class body</p></li>\n<li><p>Any class reference (must be in module's top-level scope)</p></li>\n<li><p>Anything with a custom <code class=\"docutils literal notranslate\"><span class=\"pre\">deconstruct()</span></code> method (<a class=\"reference internal\" href=\"#custom-deconstruct-method\"><span class=\"std std-ref\">see below</span></a>)</p></li>\n</ul>\n<aside class=\"version-note version-changed\" data-version=\"3.2\">\n<p class=\"version-note-title\">Changed in Django 3.2</p><p>Serialization support for pure and concrete path objects from\n<a class=\"reference external\" href=\"https://docs.python.org/3/library/pathlib.html#module-pathlib\" title=\"(di Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">pathlib</span></code></a>, and <a class=\"reference external\" href=\"https://docs.python.org/3/library/os.html#os.PathLike\" title=\"(di Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">os.PathLike</span></code></a> instances was added.</p>\n</aside>\n<p>Django tidak dapat diserialkan:</p>\n<ul class=\"simple\">\n<li><p>Kelas bersarang</p></li>\n<li><p>Arbitrary class instances (e.g. <code class=\"docutils literal notranslate\"><span class=\"pre\">MyClass(4.3,</span> <span class=\"pre\">5.7)</span></code>)</p></li>\n<li><p>Lambdas</p></li>\n</ul>\n<section id=\"custom-serializers\">\n<span id=\"custom-migration-serializers\"></span><h3>Custom serializers<a class=\"heading-anchor\" href=\"#custom-serializers\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>You can serialize other types by writing a custom serializer. For example, if\nDjango didn't serialize <a class=\"reference external\" href=\"https://docs.python.org/3/library/decimal.html#decimal.Decimal\" title=\"(di Python v3.14)\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Decimal</span></code></a> by default, you could do\nthis:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">decimal</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Decimal</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.db.migrations.serializer</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">BaseSerializer</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.db.migrations.writer</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">MigrationWriter</span>\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">DecimalSerializer</span><span class=\"p\">(</span><span class=\"n\">BaseSerializer</span><span class=\"p\">):</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">serialize</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"nb\">repr</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">value</span><span class=\"p\">),</span> <span class=\"p\">{</span><span class=\"s1\">&#39;from decimal import Decimal&#39;</span><span class=\"p\">}</span>\n\n<span class=\"n\">MigrationWriter</span><span class=\"o\">.</span><span class=\"n\">register_serializer</span><span class=\"p\">(</span><span class=\"n\">Decimal</span><span class=\"p\">,</span> <span class=\"n\">DecimalSerializer</span><span class=\"p\">)</span>\n</code></pre></div>\n<p>The first argument of <code class=\"docutils literal notranslate\"><span class=\"pre\">MigrationWriter.register_serializer()</span></code> is a type or\niterable of types that should use the serializer.</p>\n<p>The <code class=\"docutils literal notranslate\"><span class=\"pre\">serialize()</span></code> method of your serializer must return a string of how the\nvalue should appear in migrations and a set of any imports that are needed in\nthe migration.</p>\n</section>\n<section id=\"adding-a-deconstruct-method\">\n<span id=\"custom-deconstruct-method\"></span><h3>Menambahkan cara <code class=\"docutils literal notranslate\"><span class=\"pre\">deconstruct()</span></code><a class=\"heading-anchor\" href=\"#adding-a-deconstruct-method\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>You can let Django serialize your own custom class instances by giving the class\na <code class=\"docutils literal notranslate\"><span class=\"pre\">deconstruct()</span></code> method. It takes no arguments, and should return a tuple\nof three things <code class=\"docutils literal notranslate\"><span class=\"pre\">(path,</span> <span class=\"pre\">args,</span> <span class=\"pre\">kwargs)</span></code>:</p>\n<ul class=\"simple\">\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">path</span></code> should be the Python path to the class, with the class name included\nas the last part (for example, <code class=\"docutils literal notranslate\"><span class=\"pre\">myapp.custom_things.MyClass</span></code>). If your\nclass is not available at the top level of a module it is not serializable.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">args</span></code> should be a list of positional arguments to pass to your class'\n<code class=\"docutils literal notranslate\"><span class=\"pre\">__init__</span></code> method. Everything in this list should itself be serializable.</p></li>\n<li><p><code class=\"docutils literal notranslate\"><span class=\"pre\">kwargs</span></code> should be a dict of keyword arguments to pass to your class'\n<code class=\"docutils literal notranslate\"><span class=\"pre\">__init__</span></code> method. Every value should itself be serializable.</p></li>\n</ul>\n<aside class=\"admonition admonition-note\" role=\"note\">\n<p class=\"admonition-title\">Catatan</p>\n<p>This return value is different from the <code class=\"docutils literal notranslate\"><span class=\"pre\">deconstruct()</span></code> method\n<a class=\"reference internal\" href=\"/id/4.0/howto/custom-model-fields/#custom-field-deconstruct-method\"><span class=\"std std-ref\">for custom fields</span></a> which returns a\ntuple of four items.</p>\n</aside>\n<p>Django will write out the value as an instantiation of your class with the\ngiven arguments, similar to the way it writes out references to Django fields.</p>\n<p>To prevent a new migration from being created each time\n<a class=\"reference internal\" href=\"/id/4.0/ref/django-admin/#django-admin-makemigrations\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">makemigrations</span></code></a> is run, you should also add a <code class=\"docutils literal notranslate\"><span class=\"pre\">__eq__()</span></code> method to\nthe decorated class. This function will be called by Django's migration\nframework to detect changes between states.</p>\n<p>As long as all of the arguments to your class' constructor are themselves\nserializable, you can use the <code class=\"docutils literal notranslate\"><span class=\"pre\">&#64;deconstructible</span></code> class decorator from\n<code class=\"docutils literal notranslate\"><span class=\"pre\">django.utils.deconstruct</span></code> to add the <code class=\"docutils literal notranslate\"><span class=\"pre\">deconstruct()</span></code> method:</p>\n<div class=\"code-block\" data-language=\"default\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Code</span><button type=\"button\" class=\"copy-button\" data-copy hidden><span class=\"copy-button-label\">Copy</span></button></div><pre role=\"group\" tabindex=\"0\" aria-label=\"Code code\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils.deconstruct</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">deconstructible</span>\n\n<span class=\"nd\">@deconstructible</span>\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">MyCustomClass</span><span class=\"p\">:</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"fm\">__init__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">foo</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">):</span>\n        <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">foo</span> <span class=\"o\">=</span> <span class=\"n\">foo</span>\n        <span class=\"o\">...</span>\n\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"fm\">__eq__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">,</span> <span class=\"n\">other</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">foo</span> <span class=\"o\">==</span> <span class=\"n\">other</span><span class=\"o\">.</span><span class=\"n\">foo</span>\n</code></pre></div>\n<p>The decorator adds logic to capture and preserve the arguments on their\nway into your constructor, and then returns those arguments exactly when\ndeconstruct() is called.</p>\n</section>\n</section>\n<section id=\"supporting-multiple-django-versions\">\n<h2>Mendukung banyak versi Django<a class=\"heading-anchor\" href=\"#supporting-multiple-django-versions\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>If you are the maintainer of a third-party app with models, you may need to\nship migrations that support multiple Django versions. In this case, you should\nalways run <a class=\"reference internal\" href=\"/id/4.0/ref/django-admin/#django-admin-makemigrations\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">makemigrations</span></code></a> <strong>with the lowest Django version you wish\nto support</strong>.</p>\n<p>The migrations system will maintain backwards-compatibility according to the\nsame policy as the rest of Django, so migration files generated on Django X.Y\nshould run unchanged on Django X.Y+1. The migrations system does not promise\nforwards-compatibility, however. New features may be added, and migration files\ngenerated with newer versions of Django may not work on older versions.</p>\n<aside class=\"admonition admonition-seealso\">\n<p class=\"admonition-title\">Lihat juga</p>\n<dl class=\"simple\">\n<dt><span class=\"xref std std-doc\">The Migrations Operations Reference 1</span></dt><dd><p>Menutupi skema API tindakan, tindakan khusus, dan menulis tindakan anda sendiri.</p>\n</dd>\n<dt><a class=\"reference internal\" href=\"/id/4.0/howto/writing-migrations/\"><span class=\"doc\">Menulis Perpindahan &quot;how-to&quot;</span></a></dt><dd><p>Explains how to structure and write database migrations for different\nscenarios you might encounter.</p>\n</dd>\n</dl>\n</aside>\n</section>","rootId":"module-django.db.migrations","toc":[{"title":"Perintah","anchor":"the-commands","children":[]},{"title":"Dukungan backend","anchor":"backend-support","children":[{"title":"PostgreSQL","anchor":"postgresql","children":[]},{"title":"MySQL","anchor":"mysql","children":[]},{"title":"SQLite","anchor":"sqlite","children":[]}]},{"title":"Alur kerja","anchor":"workflow","children":[{"title":"Kendali versi","anchor":"version-control","children":[]}]},{"title":"Transaksi","anchor":"transactions","children":[]},{"title":"Ketergantungan","anchor":"dependencies","children":[]},{"title":"Berkas perpindahan","anchor":"migration-files","children":[{"title":"Bidang penyesuaian","anchor":"custom-fields","children":[]},{"title":"Pengelola model","anchor":"model-managers","children":[]},{"title":"Perpindahan awal","anchor":"initial-migrations","children":[]},{"title":"History consistency","anchor":"history-consistency","children":[]}]},{"title":"Menambahkan perpindahan ke aplikasi","anchor":"adding-migrations-to-apps","children":[]},{"title":"Reversing migrations","anchor":"reversing-migrations","children":[]},{"title":"Historical models","anchor":"historical-models","children":[]},{"title":"Pertimbangkan ketika memindahkan bidang model","anchor":"considerations-when-removing-model-fields","children":[]},{"title":"Perpindahan Data","anchor":"data-migrations","children":[{"title":"Mengakses model dari aplikasi lain","anchor":"accessing-models-from-other-apps","children":[]},{"title":"Perpindahan lebih lanjut","anchor":"more-advanced-migrations","children":[]}]},{"title":"Menginjak perpindahan","anchor":"squashing-migrations","children":[]},{"title":"Serializing values","anchor":"serializing-values","children":[{"title":"Custom serializers","anchor":"custom-serializers","children":[]},{"title":"Menambahkan cara deconstruct()","anchor":"adding-a-deconstruct-method","children":[]}]},{"title":"Mendukung banyak versi Django","anchor":"supporting-multiple-django-versions","children":[]}],"breadcrumbs":[{"docname":"topics/index","title":"Menggunakan Django","url":"/id/4.0/topics/"}],"prev":{"docname":"topics/class-based-views/mixins","title":"Menggunakan mixin dengan tampilan berdasarkan-kelas","url":"/id/4.0/topics/class-based-views/mixins/"},"next":{"docname":"topics/files","title":"Mengelola berkas","url":"/id/4.0/topics/files/"},"formats":{"html":"/id/4.0/topics/migrations/","markdown":"/id/4.0/topics/migrations.md","json":"/id/4.0/topics/migrations.json"},"source":"https://github.com/django/django/blob/stable/4.0.x/docs/topics/migrations.txt","official":"https://docs.djangoproject.com/id/4.0/topics/migrations/","inVersions":["6.1","6.0","5.2","5.1","5.0","4.2","4.1","4.0","3.2","3.1","3.0","2.2","2.1","2.0","1.11","1.10","1.9"],"inLocales":["en","zh-hans","fr","ja","id","it","pt-br","ko","es","el","pl"]}