{"title":"How to integrate Django with a legacy database","version":"5.2","locale":"pl","docname":"howto/legacy-databases","url":"/pl/5.2/howto/legacy-databases/","canonical":"https://djangodocs.dev/pl/5.2/howto/legacy-databases/","summary":"While Django is best suited for developing new applications, it’s quite possible to integrate it into legacy databases. Django includes a couple of utilities to…","html":"<h1>How to integrate Django with a legacy database<a class=\"heading-anchor\" href=\"#how-to-integrate-django-with-a-legacy-database\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>While Django is best suited for developing new applications, it’s quite\npossible to integrate it into legacy databases. Django includes a couple of\nutilities to automate as much of this process as possible.</p>\n<p>This document assumes you know the Django basics, as covered in the\n<a class=\"reference internal\" href=\"/pl/5.2/intro/tutorial01/\"><span class=\"doc\">tutorial</span></a>.</p>\n<p>Once you’ve got Django set up, you’ll follow this general process to integrate\nwith an existing database.</p>\n<section id=\"give-django-your-database-parameters\">\n<h2>Podaj Django swoje parametry bazy danych<a class=\"heading-anchor\" href=\"#give-django-your-database-parameters\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>You’ll need to tell Django what your database connection parameters are, and\nwhat the name of the database is. Do that by editing the <a class=\"reference internal\" href=\"/pl/5.2/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DATABASES</span></code></a>\nsetting and assigning values to the following keys for the <code class=\"docutils literal notranslate\"><span class=\"pre\">'default'</span></code>\nconnection:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/pl/5.2/ref/settings/#std-setting-NAME\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">NAME</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/pl/5.2/ref/settings/#std-setting-DATABASE-ENGINE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">ENGINE</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/pl/5.2/ref/settings/#std-setting-USER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USER</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/pl/5.2/ref/settings/#std-setting-PASSWORD\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">PASSWORD</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/pl/5.2/ref/settings/#std-setting-HOST\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">HOST</span></code></a></p></li>\n<li><p><a class=\"reference internal\" href=\"/pl/5.2/ref/settings/#std-setting-PORT\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">PORT</span></code></a></p></li>\n</ul>\n</section>\n<section id=\"auto-generate-the-models\">\n<h2>Auto-generate the models<a class=\"heading-anchor\" href=\"#auto-generate-the-models\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Django comes with a utility called <a class=\"reference internal\" href=\"/pl/5.2/ref/django-admin/#django-admin-inspectdb\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">inspectdb</span></code></a> that can create models\nby introspecting an existing database. You can view the output by running this\ncommand:</p>\n<div class=\"code-block\" data-language=\"shell\"><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=\"w\"> </span>python<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>inspectdb\n</code></pre></div>\n<p>Save this as a file by using standard Unix output redirection:</p>\n<div class=\"code-block\" data-language=\"shell\"><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=\"w\"> </span>python<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>inspectdb<span class=\"w\"> </span>&gt;<span class=\"w\"> </span>models.py\n</code></pre></div>\n<p>This feature is meant as a shortcut, not as definitive model generation. See the\n<a class=\"reference internal\" href=\"/pl/5.2/ref/django-admin/#django-admin-inspectdb\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">documentation</span> <span class=\"pre\">of</span> <span class=\"pre\">inspectdb</span></code></a> for more information.</p>\n<p>Once you’ve cleaned up your models, name the file <code class=\"docutils literal notranslate\"><span class=\"pre\">models.py</span></code> and put it in\nthe Python package that holds your app. Then add the app to your\n<a class=\"reference internal\" href=\"/pl/5.2/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a> setting.</p>\n<p>By default, <a class=\"reference internal\" href=\"/pl/5.2/ref/django-admin/#django-admin-inspectdb\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">inspectdb</span></code></a> creates unmanaged models. That is,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">managed</span> <span class=\"pre\">=</span> <span class=\"pre\">False</span></code> in the model’s <code class=\"docutils literal notranslate\"><span class=\"pre\">Meta</span></code> class tells Django not to manage\neach table’s creation, modification, and deletion:</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\">Person</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=\"nb\">id</span> <span class=\"o\">=</span> <span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">IntegerField</span><span class=\"p\">(</span><span class=\"n\">primary_key</span><span class=\"o\">=</span><span class=\"kc\">True</span><span class=\"p\">)</span>\n    <span class=\"n\">first_name</span> <span class=\"o\">=</span> <span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">CharField</span><span class=\"p\">(</span><span class=\"n\">max_length</span><span class=\"o\">=</span><span class=\"mi\">70</span><span class=\"p\">)</span>\n\n    <span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Meta</span><span class=\"p\">:</span>\n        <span class=\"n\">managed</span> <span class=\"o\">=</span> <span class=\"kc\">False</span>\n        <span class=\"n\">db_table</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;CENSUS_PERSONS&quot;</span>\n</code></pre></div>\n<p>If you do want to allow Django to manage the table’s lifecycle, you’ll need to\nchange the <a class=\"reference internal\" href=\"/pl/5.2/ref/models/options/#django.db.models.Options.managed\" title=\"django.db.models.Options.managed\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">managed</span></code></a> option above to <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code>\n(or remove it because <code class=\"docutils literal notranslate\"><span class=\"pre\">True</span></code> is its default value).</p>\n</section>\n<section id=\"install-the-core-django-tables\">\n<h2>Zainstaluj podstawowe tabele Django<a class=\"heading-anchor\" href=\"#install-the-core-django-tables\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Next, run the <a class=\"reference internal\" href=\"/pl/5.2/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">migrate</span></code></a> command to install any extra needed database\nrecords such as admin permissions and content types:</p>\n<div class=\"code-block\" data-language=\"shell\"><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=\"w\"> </span>python<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>migrate\n</code></pre></div>\n</section>\n<section id=\"test-and-tweak\">\n<h2>Przetestuj i popraw<a class=\"heading-anchor\" href=\"#test-and-tweak\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Those are the basic steps – from here you’ll want to tweak the models Django\ngenerated until they work the way you’d like. Try accessing your data via the\nDjango database API, and try editing objects via Django’s admin site, and edit\nthe models file accordingly.</p>\n</section>","rootId":"how-to-integrate-django-with-a-legacy-database","toc":[{"title":"Podaj Django swoje parametry bazy danych","anchor":"give-django-your-database-parameters","children":[]},{"title":"Auto-generate the models","anchor":"auto-generate-the-models","children":[]},{"title":"Zainstaluj podstawowe tabele Django","anchor":"install-the-core-django-tables","children":[]},{"title":"Przetestuj i popraw","anchor":"test-and-tweak","children":[]}],"breadcrumbs":[{"docname":"howto/index","title":"How-to guides","url":"/pl/5.2/howto/"}],"prev":{"docname":"howto/initial-data","title":"How to provide initial data for models","url":"/pl/5.2/howto/initial-data/"},"next":{"docname":"howto/custom-model-fields","title":"Jak utworzyć własne modele pól","url":"/pl/5.2/howto/custom-model-fields/"},"formats":{"html":"/pl/5.2/howto/legacy-databases/","markdown":"/pl/5.2/howto/legacy-databases.md","json":"/pl/5.2/howto/legacy-databases.json"},"source":"https://github.com/django/django/blob/stable/5.2.x/docs/howto/legacy-databases.txt","official":"https://docs.djangoproject.com/pl/5.2/howto/legacy-databases/","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"],"inLocales":["en","sv","zh-hans","ga","fr","ja","id","it","pt-br","ko","es","el","pl"]}