{"id":75904,"date":"2020-01-20T12:31:02","date_gmt":"2020-01-20T07:01:02","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=75904"},"modified":"2024-04-12T14:17:42","modified_gmt":"2024-04-12T08:47:42","slug":"integrating-with-a-legacy-database","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-a-legacy-database\/","title":{"rendered":"Integrating with a Legacy Database"},"content":{"rendered":"<p>Django\u2019s database layer generates SQL schemas from Python code \u2014 but with a legacy database, you already have the SQL schemas. In such a case, you\u2019ll need to create models for your existing database tables. For this purpose, Django comes with a tool that can generate model code by reading your database table layouts. This tool is called inspectdb, and you can call it by executing the command manage.py inspectdb.<\/p>\n<p><strong>Using <\/strong><strong>inspectdb<\/strong> &#8211; The inspectdb utility introspects the database pointed to by your settings file, determines a Django model representation for each of your tables, and prints the Python model code to standard output. Here\u2019s a walk-through of a typical legacy database integration process from scratch. The only assumptions are that Django is installed and that you have a legacy database.<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>Create a Django project by running django-admin.pystartprojectmysite (where mysite is your project\u2019s name). We\u2019ll use mysite as the project name in this example.<\/li>\n<li>Edit the settings file in that project, mysite\/settings.py, to tell Django what your database connection parameters are and what the name of the database is. Specifically, provide the DATABASE_NAME, DATABASE_ENGINE, DATABASE_USER, DATABASE_PASSWORD, DATABASE_HOST, and DATABASE_PORT Note that some of these settings are optional.<\/li>\n<li>Create a Django application within your project by running pythonmysite\/manage.pystartappmyapp (where myapp is your application\u2019s name). We\u2019ll use myapp as the application name here.<\/li>\n<li>Run the command pythonmysite\/manage.pyinspectdb. This will examine the tables in the DATABASE_NAME database and print the generated model class for each table. Take a look at the output to get an idea of what inspectdb can do.<\/li>\n<li>Save the output to the py file within your application by using standard shell output redirection:<\/li>\n<\/ul>\n<p>python mysite\/manage.py inspectdb &gt; mysite\/myapp\/models.py<\/p>\n<ul>\n<li>Edit the mysite\/myapp\/models.py file to clean up the generated models and make any necessary customizations. We\u2019ll give some hints for this in the next section.<\/li>\n<\/ul>\n<p><strong>Cleaning Up Generated Models<\/strong> &#8211; As you might expect, the database introspection isn\u2019t perfect, and you\u2019ll need to do some light cleanup of the resulting model code. Here are a few pointers for dealing with the generated models:<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>Each database table is converted to a model class (i.e., there is a one-to-one mapping between database tables and model classes). This means that you\u2019ll need to refactor the models for any many-to-many join tables into ManyToManyField<\/li>\n<li>Each generated model has an attribute for every field, including id primary key fields. However, recall that Django automatically adds an id primary key field if a model doesn\u2019t have a primary key. Thus, you\u2019ll want to remove any lines that look like this:<\/li>\n<\/ul>\n<p>id = models.IntegerField(primary_key=True)<\/p>\n<p>Not only are these lines redundant, but also they can cause problems if your application will be adding <em>new<\/em> records to these tables. The inspectdb command cannot detect whether a field is autoincremented, so it\u2019s up to you to change this to AutoField, if necessary.<\/p>\n<ul>\n<li>Each field\u2019s type (e.g., CharField, DateField) is determined by looking at the database column type (e.g., VARCHAR, DATE). If inspectdb cannot map a column\u2019s type to a model field type, it will use TextField and will insert the Python comment &#8216;Thisfieldtypeisa&#8217; next to the field in the generated model. Keep an eye out for that, and change the field type accordingly if needed.<\/li>\n<\/ul>\n<p>If a field in your database has no good Django equivalent, you can safely leave it off. The Django model layer is not required to include every field in your table(s).<\/p>\n<p>&nbsp;<\/p>\n<ul>\n<li>If a database column name is a Python reserved word (such as pass, class, or for), inspectdb will append &#8216;_field&#8217; to the attribute name and set the db_column attribute to the real field name (e.g., pass, class, or for).<\/li>\n<\/ul>\n<p>For example, if a table has an INT column called for, the generated model will have a field like this:<\/p>\n<p>for_field = models.IntegerField(db_column=&#8217;for&#8217;)<\/p>\n<p>inspectdb will insert the Python comment &#8216;Field renamed because it was a Python reserved word.&#8217; next to the field.<\/p>\n<ul>\n<li>If your database contains tables that refer to other tables (as most databases do), you might need to rearrange the order of the generated models so that models that refer to other models are ordered properly. For example, if model Book has a ForeignKey to model Author, model Author should be defined before model Book. If you need to create a relationship on a model that has not yet been defined, you can use the name of the model, rather than the model object itself.<\/li>\n<li>inspectdb detects primary keys for PostgreSQL, MySQL, and SQLite. That is, it inserts primary_key=True where appropriate. For other databases, you\u2019ll need to insert primary_key=True for at least one field in each model, because Django models are required to have a primary_key=True<\/li>\n<li>Foreign-key detection only works with PostgreSQL and with certain types of MySQL tables. In other cases, foreign-key fields will be generated as IntegerField&#8220;s,assumingtheforeign-keycolumnwasan&#8220;INT<\/li>\n<\/ul>\n\n\n<p><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/certified-django-developer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Back to Tutorial<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Django\u2019s database layer generates SQL schemas from Python code \u2014 but with a legacy database, you already have the SQL schemas. In such a case, you\u2019ll need to create models for your existing database tables. For this purpose, Django comes with a tool that can generate model code by reading your database table layouts. This&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[8655],"tags":[8856],"class_list":["post-75904","page","type-page","status-publish","hentry","category-django-web-development","tag-integrating-with-a-legacy-database"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Integrating with a Legacy Database - Tutorial<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-a-legacy-database\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Integrating with a Legacy Database - Tutorial\" \/>\n<meta property=\"og:description\" content=\"Django\u2019s database layer generates SQL schemas from Python code \u2014 but with a legacy database, you already have the SQL schemas. In such a case, you\u2019ll need to create models for your existing database tables. For this purpose, Django comes with a tool that can generate model code by reading your database table layouts. This...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-a-legacy-database\/\" \/>\n<meta property=\"og:site_name\" content=\"Tutorial\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/vskills.in\/\" \/>\n<meta property=\"article:modified_time\" content=\"2024-04-12T08:47:42+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-a-legacy-database\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-a-legacy-database\/\",\"name\":\"Integrating with a Legacy Database - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2020-01-20T07:01:02+00:00\",\"dateModified\":\"2024-04-12T08:47:42+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-a-legacy-database\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-a-legacy-database\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-a-legacy-database\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Integrating with a Legacy Database\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"name\":\"Tutorial\",\"description\":\"Vskills - A initiative in elearning and certification\",\"publisher\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#organization\",\"name\":\"Vskills\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"contentUrl\":\"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg\",\"width\":73,\"height\":55,\"caption\":\"Vskills\"},\"image\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/vskills.in\/\",\"https:\/\/x.com\/vskills_in\",\"https:\/\/www.linkedin.com\/company-beta\/1371554\/\",\"https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Integrating with a Legacy Database - Tutorial","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-a-legacy-database\/","og_locale":"en_US","og_type":"article","og_title":"Integrating with a Legacy Database - Tutorial","og_description":"Django\u2019s database layer generates SQL schemas from Python code \u2014 but with a legacy database, you already have the SQL schemas. In such a case, you\u2019ll need to create models for your existing database tables. For this purpose, Django comes with a tool that can generate model code by reading your database table layouts. This...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-a-legacy-database\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:47:42+00:00","twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-a-legacy-database\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-a-legacy-database\/","name":"Integrating with a Legacy Database - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2020-01-20T07:01:02+00:00","dateModified":"2024-04-12T08:47:42+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-a-legacy-database\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-a-legacy-database\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/integrating-with-a-legacy-database\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Integrating with a Legacy Database"}]},{"@type":"WebSite","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","name":"Tutorial","description":"Vskills - A initiative in elearning and certification","publisher":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.vskills.in\/certification\/tutorial\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#organization","name":"Vskills","url":"https:\/\/www.vskills.in\/certification\/tutorial\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","contentUrl":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-content\/uploads\/2017\/07\/vskills-min-logo.jpg","width":73,"height":55,"caption":"Vskills"},"image":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/vskills.in\/","https:\/\/x.com\/vskills_in","https:\/\/www.linkedin.com\/company-beta\/1371554\/","https:\/\/www.youtube.com\/channel\/UCMWnscxPwRF_PqXo9B7q_Tw"]}]}},"_links":{"self":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75904","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/comments?post=75904"}],"version-history":[{"count":4,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75904\/revisions"}],"predecessor-version":[{"id":83430,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75904\/revisions\/83430"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=75904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=75904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=75904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}