{"id":75310,"date":"2020-01-18T14:17:47","date_gmt":"2020-01-18T08:47:47","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=75310"},"modified":"2024-04-12T14:17:13","modified_gmt":"2024-04-12T08:47:13","slug":"installing-the-model","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/installing-the-model\/","title":{"rendered":"Installing the Model"},"content":{"rendered":"<p>We\u2019ve written the code; now let\u2019s create the tables in our database. In order to do that, the first step is to <em>activate<\/em> these models in our Django project. We do that by adding the books app to the list of installed apps in the settings file. Edit the settings.py file again, and look for the INSTALLED_APPS setting. INSTALLED_APPS tells Django which apps are activated for a given project. By default, it looks something like this:<\/p>\n<pre>INSTALLED_APPS = [\n'django.contrib.admin',\n'django.contrib.auth',\n'django.contrib.contenttypes',\n'django.contrib.sessions',\n'django.contrib.messages',\n'django.contrib.staticfiles',\n]<\/pre>\n<p class=\"VSKILLbodytext\"><span lang=\"EN-US\">To register our \u201cbooks\u201d app, add &#8216;books.apps.BooksConfig&#8217; to INSTALLED_APPS, so the setting ends up looking like this:<\/span><\/p>\n<pre class=\"VSKILLbodytext\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/span><span lang=\"EN-US\">INSTALLED_APPS = [\n<\/span><span lang=\"EN-US\">'books.apps.BooksConfig',\n<\/span><span lang=\"EN-US\">'django.contrib.admin',\n<\/span><span lang=\"EN-US\">'django.contrib.auth',\n<\/span><span lang=\"EN-US\">'django.contrib.contenttypes',\n<\/span><span lang=\"EN-US\">'django.contrib.sessions',\n<\/span><span lang=\"EN-US\">'django.contrib.messages',\n<\/span><span lang=\"EN-US\">'django.contrib.staticfiles',\n<\/span><span lang=\"EN-US\">]<\/span><\/pre>\n<p>Each app in INSTALLED_APPS is represented by its full Python path \u2013 that is, the path of packages, separated by dots, leading to the app package. The dotted path in this case points to the BooksConfig class that Django created for you in the apps.py file. Well will look at app configurations in more detail later in the book.<\/p>\n<p>Now that the Django app has been activated in the settings file, we can create the database tables in our database. First, let\u2019s validate the models by running this command:<\/p>\n<pre>python manage.py check<\/pre>\n<p>The check command runs the Django system check framework \u2013 a set of static checks for validating Django projects. If all is well, you\u2019ll see the message System check identified no issues (0 silenced). If you don\u2019t, make sure you typed in the model code correctly. The error output should give you helpful information about what was wrong with the code. Any time you think you have problems with your models, run python manage.py check. It tends to catch all the common model problems.<\/p>\n<p>If your models are valid, run the following command to tell Django that you have made some changes to your models (in this case, you have made a new one):<\/p>\n<pre>\u00a0 python manage.py makemigrations books<\/pre>\n<p>You should see something similar to the following:<\/p>\n<pre>Migrations for 'books':\n0001_initial.py:\n- Create model Author\n- Create model Book\n- Create model Publisher\n- Add field publisher to book<\/pre>\n<p>Migrations are how Django stores changes to your models (and thus your database schema) \u2013 they\u2019re just files on disk. In this instance, you will find a file names 0001_initial.py in the \\migrations folder of the books app. The migrate command will take your latest migration file and update your database schema automatically, but first, let\u2019s see what SQL that migration would run. The sqlmigrate command takes migration names and returns their SQL:<\/p>\n<pre>python manage.py sqlmigrate books 0001<\/pre>\n<p>You should see something similar to the following (reformatted for readability):<\/p>\n<pre>BEGIN;<\/pre>\n<pre>CREATE TABLE \"books_author\" (\n\"id\" integer NOT NULL PRIMARY KEY AUTOINCREMENT,\n\"first_name\" varchar(30) NOT NULL,\n\"last_name\" varchar(40) NOT NULL,\n\"email\" varchar(254) NOT NULL\n);\nCREATE TABLE \"books_book\" (\n\"id\" integer NOT NULL PRIMARY KEY AUTOINCREMENT,\n\"title\" varchar(100) NOT NULL,\n\"publication_date\" date NOT NULL\n);\nCREATE TABLE \"books_book_authors\" (\n\"id\" integer NOT NULL PRIMARY KEY AUTOINCREMENT,\n\"book_id\" integer NOT NULL REFERENCES \"books_book\" (\"id\"),\n\"author_id\" integer NOT NULL REFERENCES \"books_author\" (\"id\"),\nUNIQUE (\"book_id\", \"author_id\")\n);\nCREATE TABLE \"books_publisher\" (\n\"id\" integer NOT NULL PRIMARY KEY AUTOINCREMENT,\n\"name\" varchar(30) NOT NULL,\n\"address\" varchar(50) NOT NULL,\n\"city\" varchar(60) NOT NULL,\n\"state_province\" varchar(30) NOT NULL,\n\"country\" varchar(50) NOT NULL,\n\"website\" varchar(200) NOT NULL\n);\nCREATE TABLE \"books_book__new\" (\n\"id\" integer NOT NULL PRIMARY KEY AUTOINCREMENT,\n\"title\" varchar(100) NOT NULL,\n\"publication_date\" date NOT NULL,\n\"publisher_id\" integer NOT NULL REFERENCES\n\"books_publisher\" (\"id\")\n);\n\nINSERT INTO \"books_book__new\" (\"id\", \"publisher_id\", \"title\",\n\"publication_date\") SELECT \"id\", NULL, \"title\", \"publication_date\" FROM\n\"books_book\";\n\nDROP TABLE \"books_book\";\n\nALTER TABLE \"books_book__new\" RENAME TO \"books_book\";\n\nCREATE INDEX \"books_book_2604cbea\" ON \"books_book\" (\"publisher_id\");\n\nCOMMIT;\n\n<\/pre>\n<p class=\"VSKILLbodytext\"><span lang=\"EN-US\">Note the following:<\/span><\/p>\n<ul>\n<li>Table names are automatically generated by combining the name of the app (books) and the lowercase name of the model (publisher, book, and author). You can override this behavior, as detailed in Appendix B.<\/li>\n<li>As we mentioned earlier, Django adds a primary key for each table automatically \u2013 the id fields. You can override this. By convention, Django appends &#8220;_id&#8221; to the foreign key field name. As you might have guessed, you can override this behavior, too.<\/li>\n<li>The foreign key relationship is made explicit by a REFERENCES statement.<\/li>\n<\/ul>\n<p>These CREATE TABLE statements are tailored to the database you\u2019re using, so database-specific field types such as auto_increment (MySQL), serial (PostgreSQL), or integer primary key (SQLite) are handled for you automatically. The same goes for quoting of column names (e.g., using double quotes or single quotes). This example output is in PostgreSQL syntax.<\/p>\n<p>The sqlmigrate command doesn\u2019t actually create the tables or otherwise touch your database \u2013 it just prints output to the screen so you can see what SQL Django would execute if you asked it. If you wanted to, you could copy and paste this SQL into your database client, however Django provides an easier way of committing the SQL to the database: the migrate command:<\/p>\n<pre>python manage.py migrate<\/pre>\n<p>Run that command, and you\u2019ll see something like this:<\/p>\n<pre>Operations to perform:\nApply all migrations: admin, auth, books, contenttypes, sessions\nRunning migrations:\nApplying books.0001_initial... OK<\/pre>\n<p>In case you get a bunch of extra migrations, this is most likely because you forgot to run the initial migrations. The first time you run migrate, Django will also create all the system tables that Django needs for the inbuilt apps.<\/p>\n<p>Migrations are Django\u2019s way of propagating changes you make to your models (adding a field, deleting a model, etc.) into your database schema. They\u2019re designed to be mostly automatic, however there are some caveats.<\/p>\n<p><a href=\"https:\/\/www.vskills.in\/certification\/tutorial\/certified-django-developer\/\" target=\"_blank\" rel=\"noopener noreferrer\">Back to Tutorial<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We\u2019ve written the code; now let\u2019s create the tables in our database. In order to do that, the first step is to activate these models in our Django project. We do that by adding the books app to the list of installed apps in the settings file. Edit the settings.py file again, and look for&#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":[8674],"class_list":["post-75310","page","type-page","status-publish","hentry","category-django-web-development","tag-installing-the-model"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Installing the Model - 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\/installing-the-model\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Installing the Model - Tutorial\" \/>\n<meta property=\"og:description\" content=\"We\u2019ve written the code; now let\u2019s create the tables in our database. In order to do that, the first step is to activate these models in our Django project. We do that by adding the books app to the list of installed apps in the settings file. Edit the settings.py file again, and look for...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/installing-the-model\/\" \/>\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:13+00:00\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"5 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\/installing-the-model\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/installing-the-model\/\",\"name\":\"Installing the Model - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2020-01-18T08:47:47+00:00\",\"dateModified\":\"2024-04-12T08:47:13+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/installing-the-model\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/installing-the-model\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/installing-the-model\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Installing the Model\"}]},{\"@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":"Installing the Model - 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\/installing-the-model\/","og_locale":"en_US","og_type":"article","og_title":"Installing the Model - Tutorial","og_description":"We\u2019ve written the code; now let\u2019s create the tables in our database. In order to do that, the first step is to activate these models in our Django project. We do that by adding the books app to the list of installed apps in the settings file. Edit the settings.py file again, and look for...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/installing-the-model\/","og_site_name":"Tutorial","article_publisher":"https:\/\/www.facebook.com\/vskills.in\/","article_modified_time":"2024-04-12T08:47:13+00:00","twitter_misc":{"Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/installing-the-model\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/installing-the-model\/","name":"Installing the Model - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2020-01-18T08:47:47+00:00","dateModified":"2024-04-12T08:47:13+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/installing-the-model\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/installing-the-model\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/installing-the-model\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Installing the Model"}]},{"@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\/75310","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=75310"}],"version-history":[{"count":4,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75310\/revisions"}],"predecessor-version":[{"id":83328,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75310\/revisions\/83328"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=75310"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=75310"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=75310"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}