{"id":75308,"date":"2020-01-18T14:17:36","date_gmt":"2020-01-18T08:47:36","guid":{"rendered":"https:\/\/www.vskills.in\/certification\/tutorial\/?p=75308"},"modified":"2024-04-12T14:17:13","modified_gmt":"2024-04-12T08:47:13","slug":"your-first-model","status":"publish","type":"page","link":"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-model\/","title":{"rendered":"Your First Model"},"content":{"rendered":"<p>As an ongoing example in this chapter and the next chapter, we\u2019ll focus on a basic book\/author\/publisher data layout. We use this as our example because the conceptual relationships between books, authors, and publishers are well known, and this is a common data layout used in introductory SQL textbooks. You\u2019re also reading a book that was written by authors and produced by a publisher!<\/p>\n<p>I\u2019ll suppose the following concepts, fields, and relationships:<\/p>\n<ul>\n<li>An author has a salutation (e.g., Mr. or Mrs.), a first name, a last name, an email address, and a headshot photo.<\/li>\n<li>A publisher has a name, a street address, a city, a state\/province, a country, and a Web site.<\/li>\n<li>A book has a title and a publication date. It also has one or more authors (a many-to-many relationship with authors) and a single publisher (a one-to-many relationship \u2014 aka foreign key \u2014 to publishers).<\/li>\n<\/ul>\n<p>The first step in using this database layout with Django is to express it as Python code. In the models.py file that was created by the startapp command, enter the following:<\/p>\n<pre>from django.db import models\n&nbsp;class Publisher(models.Model):\n&nbsp;&nbsp;&nbsp; name = models.CharField(maxlength=30)\n&nbsp;&nbsp;&nbsp; address = models.CharField(maxlength=50)\n&nbsp;&nbsp;&nbsp; city = models.CharField(maxlength=60)\n&nbsp;&nbsp;&nbsp; state_province = models.CharField(maxlength=30)\n&nbsp;&nbsp;&nbsp; country = models.CharField(maxlength=50)\n&nbsp;&nbsp;&nbsp; website = models.URLField()\n\n&nbsp;class Author(models.Model):\n&nbsp;&nbsp;&nbsp; salutation = models.CharField(maxlength=10)\n&nbsp;&nbsp;&nbsp; first_name = models.CharField(maxlength=30)\n&nbsp;&nbsp;&nbsp; last_name = models.CharField(maxlength=40)\n&nbsp;&nbsp;&nbsp; email = models.EmailField()\n&nbsp;&nbsp;&nbsp; headshot = models.ImageField(upload_to='\/tmp')\n\n&nbsp;class Book(models.Model):\n&nbsp;&nbsp;&nbsp; title = models.CharField(maxlength=100)\n&nbsp;&nbsp;&nbsp; authors = models.ManyToManyField(Author)\n&nbsp;&nbsp;&nbsp; publisher = models.ForeignKey(Publisher)\n&nbsp;&nbsp;&nbsp; publication_date = models.DateField()\n\n<\/pre>\n<p class=\"VSKILLbodytext\">Let\u2019s quickly examine this code to cover the basics. The first thing to notice is that each model is represented by a Python class that is a subclass of <span class=\"pre\"><span style=\"font-size: 10.0pt; font-family: 'Courier New';\">django.db.models.Model<\/span><\/span>. The parent class, <span class=\"pre\"><span style=\"font-size: 10.0pt; font-family: 'Courier New';\">Model<\/span><\/span>, contains all the machinery necessary to make these objects capable of interacting with a database \u2014 and that leaves our models responsible solely for defining their fields, in a nice and compact syntax.<\/p>\n<p class=\"VSKILLbodytext\">Believe it or not, this is all the code we need to write to have basic data access with Django.Each model generally corresponds to a single database table, and each attribute on a model generally corresponds to a column in that database table. The attribute name corresponds to the column\u2019s name, and the type of field (e.g., <span class=\"pre\"><span style=\"font-size: 10.0pt; font-family: 'Courier New';\">CharField<\/span><\/span>) corresponds to the database column type (e.g., <span class=\"pre\"><span style=\"font-size: 10.0pt; font-family: 'Courier New';\">varchar<\/span><\/span>). For example, the <span class=\"pre\"><span style=\"font-size: 10.0pt; font-family: 'Courier New';\">Publisher<\/span><\/span> model is equivalent to the following table (assuming PostgreSQL <span class=\"pre\"><span style=\"font-size: 10.0pt; font-family: 'Courier New';\">CREATE<\/span><\/span><tt> <\/tt><span class=\"pre\"><span style=\"font-size: 10.0pt; font-family: 'Courier New';\">TABLE<\/span><\/span> syntax):<\/p>\n<pre class=\"VSKILLbodytext\">CREATE TABLE \"books_publisher\" (\n&nbsp;&nbsp;&nbsp; \"id\" serial NOT NULL PRIMARY KEY,\n&nbsp;&nbsp;&nbsp; \"name\" varchar(30) NOT NULL,\n&nbsp;&nbsp;&nbsp; \"address\" varchar(50) NOT NULL,\n&nbsp;&nbsp;&nbsp; \"city\" varchar(60) NOT NULL,\n&nbsp;&nbsp;&nbsp; \"state_province\" varchar(30) NOT NULL,\n&nbsp;&nbsp;&nbsp; \"country\" varchar(50) NOT NULL,\n&nbsp;&nbsp;&nbsp; \"website\" varchar(200) NOT NULL\n);\n\n<\/pre>\n<p>Indeed, Django can generate that CREATE TABLE statement automatically, as we\u2019ll show in a moment. The exception to the one-class-per-database-table rule is the case of many-to-many relationships. In our example models, Book has a ManyToManyField called authors. This designates that a book has one or many authors, but the Book database table doesn\u2019t get an authors column. Rather, Django creates an additional table \u2014 a many-to-many \u201cjoin table\u201d \u2014 that handles the mapping of books to authors.<\/p>\n<p class=\"VSKILLbodytext\">For a full list of field types and model syntax options. Finally, note we haven\u2019t explicitly defined a primary key in any of these models. Unless you instruct it otherwise, Django automatically gives every model an integer primary key field called id. Each Django model is required to have a single-column primary key.<\/p>\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>As an ongoing example in this chapter and the next chapter, we\u2019ll focus on a basic book\/author\/publisher data layout. We use this as our example because the conceptual relationships between books, authors, and publishers are well known, and this is a common data layout used in introductory SQL textbooks. You\u2019re also reading a book that&#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":[8673],"class_list":["post-75308","page","type-page","status-publish","hentry","category-django-web-development","tag-your-first-model"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Your First 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\/your-first-model\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Your First Model - Tutorial\" \/>\n<meta property=\"og:description\" content=\"As an ongoing example in this chapter and the next chapter, we\u2019ll focus on a basic book\/author\/publisher data layout. We use this as our example because the conceptual relationships between books, authors, and publishers are well known, and this is a common data layout used in introductory SQL textbooks. You\u2019re also reading a book that...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-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=\"3 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\/your-first-model\/\",\"url\":\"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-model\/\",\"name\":\"Your First Model - Tutorial\",\"isPartOf\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/#website\"},\"datePublished\":\"2020-01-18T08:47:36+00:00\",\"dateModified\":\"2024-04-12T08:47:13+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-model\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-model\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-model\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.vskills.in\/certification\/tutorial\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Your First 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":"Your First 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\/your-first-model\/","og_locale":"en_US","og_type":"article","og_title":"Your First Model - Tutorial","og_description":"As an ongoing example in this chapter and the next chapter, we\u2019ll focus on a basic book\/author\/publisher data layout. We use this as our example because the conceptual relationships between books, authors, and publishers are well known, and this is a common data layout used in introductory SQL textbooks. You\u2019re also reading a book that...","og_url":"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-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":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-model\/","url":"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-model\/","name":"Your First Model - Tutorial","isPartOf":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/#website"},"datePublished":"2020-01-18T08:47:36+00:00","dateModified":"2024-04-12T08:47:13+00:00","breadcrumb":{"@id":"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-model\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.vskills.in\/certification\/tutorial\/your-first-model\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.vskills.in\/certification\/tutorial\/your-first-model\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.vskills.in\/certification\/tutorial\/"},{"@type":"ListItem","position":2,"name":"Your First 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\/75308","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=75308"}],"version-history":[{"count":4,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75308\/revisions"}],"predecessor-version":[{"id":83326,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/pages\/75308\/revisions\/83326"}],"wp:attachment":[{"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/media?parent=75308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/categories?post=75308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.vskills.in\/certification\/tutorial\/wp-json\/wp\/v2\/tags?post=75308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}