{"id":7148,"date":"2020-07-24T11:43:38","date_gmt":"2020-07-24T11:43:38","guid":{"rendered":"https:\/\/www.askpython.com\/?p=7148"},"modified":"2020-07-24T11:45:59","modified_gmt":"2020-07-24T11:45:59","slug":"django-models","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/django\/django-models","title":{"rendered":"Django Models &#8211; A Complete Beginner&#8217;s Guide"},"content":{"rendered":"\n<p>Django models are classes that represent a table or collection in our Database. It contains all the information regarding the table. These models are stored together in Django in a file models.py in our <a href=\"https:\/\/www.askpython.com\/django\/django-app-structure-project-structure\" class=\"rank-math-link\">Django App<\/a>.<\/p>\n\n\n\n<p>There can be many different models for different DB containing different information like User DB, Book DB, or any other table required by the web application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Basic Structure of Django Models<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nclass Modelname(models.Model):\n\t&#039;&#039;&#039;A class representing a model derived from Model class&#039;&#039;&#039;\n    #Fields\n    Field_name = models.CharField(max_length = , hepl_text = , choices = , Null etc)\n\n    #Metadata\n    class Meta:\n        Ordering = &#x5B;\u2018field_name\u2019]\n\n    #Methods\n    def __str__(self):\n\t    return &#039;&lt;readable_name&gt;&#039;\n\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">1. Fields in a Model<\/h3>\n\n\n\n<p>A particular model can have any number of fields, these represent the various attributes of the database table.<\/p>\n\n\n\n<p>There can be many different types of fields<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>CharField<\/li><li>IntegerValue<\/li><li>Email<\/li><\/ul>\n\n\n\n<p>These fields can also take arguments like <\/p>\n\n\n\n<ol class=\"wp-block-list\"><li><strong>max_length<\/strong> &#8211; The max number of characters possible.<\/li><li><strong>Primary key <\/strong>&#8211; This tells Django that this field is going to be unique for all the entries.<\/li><li><strong>Foreign key <\/strong>&#8211; This is used to connect one model with another model.<\/li><li><strong>NULL <\/strong>&#8211; If true, will convert empty fields into null value, <strong>char field take them as empty strings<\/strong><\/li><li><strong>Choices <\/strong>&#8211; If the field has to be only one of the given options(like a option box)<\/li><li><strong>Blank<\/strong> &#8211; If true, the field can be allowed to be blank,otherwise has to be filled.<\/li><\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">2. Model Metadata<\/h3>\n\n\n\n<p>This metadata has various features; one of the most important being the <strong>ordering<\/strong>. It allows you to show the responses in a particular order in the database when you give a request for that. <\/p>\n\n\n\n<p><strong>The syntax is as follows<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nordering &#x5B;&#039;&lt;Name&gt;&#039;]\n<\/pre><\/div>\n\n\n<p>This tells models to arrange the elements in the DB in the order according to the <strong>&lt;name&gt;<\/strong> ie could be alphabetical if <strong>&lt;name&gt;<\/strong> is <strong>CharField<\/strong> or could be <strong>Numerical<\/strong> ascending <strong>order<\/strong> if its <strong>IntergerField<\/strong> etc.<\/p>\n\n\n\n<p>if the syntax is used with <strong>&#8220;-&#8220;<\/strong> sign, this means that the ordering should be in the reverse order<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nordering &#x5B;-&#039;&lt;name&gt;&#039;]\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. <strong>Methods in Django Models<\/strong><\/h3>\n\n\n\n<p>These are used for better client interface, that is for better presentation etc.<\/p>\n\n\n\n<p><strong>__str__ <\/strong>, for example, tells you what to show you <strong>(like a short name)<\/strong>  in the <strong>admin site<\/strong> for each particular element in the database. (instead of showing the full information)<\/p>\n\n\n\n<p>Now let us make a project of Books, having the following information:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>A homepage with information about all the books <\/li><li>Webpages containing the information of each book. (<strong>we saw that in <a href=\"https:\/\/www.askpython.com\/django\/django-url-mapping\" class=\"rank-math-link\">Django URL mapping<\/a>) <\/strong><\/li><\/ul>\n\n\n\n<p>For that, make an App named books using the knowledge gained in the previous articles<strong>.<\/strong> <\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. <strong>Creating Our First Django Model<\/strong><\/h2>\n\n\n\n<p>In the books\/models.py let\u2019s make a model DB having Title, Price, and Genre as attributes with metadata ordering of Title.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"953\" height=\"331\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/BookModel.png\" alt=\"BookModel\" class=\"wp-image-7178\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/BookModel.png 953w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/BookModel-300x104.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/BookModel-768x267.png 768w\" sizes=\"auto, (max-width: 953px) 100vw, 953px\" \/><figcaption>BookModel<\/figcaption><\/figure><\/div>\n\n\n\n<p><strong>Now to we need to create this table in our database. By default, Django uses the SQLite database engine. <\/strong>For now, we will use this DB itself.<\/p>\n\n\n\n<p>You can check the DB you are using from the <strong>settings.py<\/strong> file under <strong>DATABASES<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"124\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/sqlite.png\" alt=\"settings.py\" class=\"wp-image-7171\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/sqlite.png 720w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/sqlite-300x52.png 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><figcaption>Settings.py<\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">2. Creating a Table in the Database<\/h2>\n\n\n\n<p>To create the table first we need to apply migrations. Write the code below in the shell<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\npython manage.py migrate\npython manage.py makemigrations &lt;app_name&gt;\nPython manage.py sqlmigrate &lt;app_name&gt; 0001\n<\/pre><\/div>\n\n\n<p><strong>And once again, run:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\nPython manage.py migrate\n<\/pre><\/div>\n\n\n<p>The output will indicate the successful creation of your database.<\/p>\n\n\n\n<p>Your model table is ready in <strong>SQLite.<\/strong> Note that<strong> <\/strong>whenever we make changes to our model we need to repeat the steps above<strong> <\/strong>in order to make changes in the table in the DB as well.<\/p>\n\n\n\n<p>Now we will learn to get\/add data in to the <strong>DB<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Retrieving information from DB<\/h2>\n\n\n\n<p>Now first we need to open python shell inside the directory, using the command:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\npython manage.py shell\n<\/pre><\/div>\n\n\n<p>Hence now we will have the python console appearing in the shell. We need to import the model table in order to add\/retrieve information from it.<\/p>\n\n\n\n<p>The syntax to import table is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nfrom &lt;app_name&gt;.models import &lt;model_name&gt;\n<\/pre><\/div>\n\n\n<p>In my case it will be like this<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"730\" height=\"128\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/python-shell.png\" alt=\"Python Shell\" class=\"wp-image-7172\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/python-shell.png 730w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/python-shell-300x53.png 300w\" sizes=\"auto, (max-width: 730px) 100vw, 730px\" \/><figcaption>Python Shell<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">1. Get all objects from a rable<\/h3>\n\n\n\n<p>For this we use the syntax<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&lt;model_name&gt;.objects.all()\n<\/pre><\/div>\n\n\n<p>Hence my code will be :<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nBookModel.objects.all()\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">2. Add information into the table<\/h3>\n\n\n\n<p>To add the information, the syntax is similar to object-oriented python syntax. Here first we create a class object with the model name and then add the attributes required.<\/p>\n\n\n\n<p><strong>To create an object, the syntax is:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nA = &lt;model_name&gt;()\n<\/pre><\/div>\n\n\n<p><strong>Example:<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nA.Title = &quot;Harry Potter&quot;\nA.Price = 120\nA.Genre = &quot;Fantasy Fiction&quot; \nA.save()\n<\/pre><\/div>\n\n\n<p>Thus we have entered our first book information. Similarly, I will add a few more.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"985\" height=\"380\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Addition-to-Db.png\" alt=\"Addition To DB\" class=\"wp-image-7179\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Addition-to-Db.png 985w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Addition-to-Db-300x116.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Addition-to-Db-768x296.png 768w\" sizes=\"auto, (max-width: 985px) 100vw, 985px\" \/><figcaption>Addition To DB<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">3. Filtering records from the DB<\/h3>\n\n\n\n<p>To filter records from a Django model Database, we run:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&lt;Model_name&gt;.objects.filter(Title =\u201d&lt;Title_Element&gt;\u201d)\n<\/pre><\/div>\n\n\n<p>Hence for example if I filter out all the Books with genre say Fiction, then<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"727\" height=\"40\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Filter.png\" alt=\"Filter\" class=\"wp-image-7180\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Filter.png 727w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Filter-300x17.png 300w\" sizes=\"auto, (max-width: 727px) 100vw, 727px\" \/><figcaption>Filter<\/figcaption><\/figure><\/div>\n\n\n\n<h3 class=\"wp-block-heading\">4. Getting complete information about an element<\/h3>\n\n\n\n<p><strong>Note:<\/strong> When we use a filter, we get the element in the short form (in a way as described by <strong>def __str__<\/strong>) But if we want full information about an element we use this method<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n&lt;model_name&gt;.objects.get(Title = &lt;&quot;title_element&quot;&gt;)\n<\/pre><\/div>\n\n\n<p>Here we can get all the information including the title. price, genre.<\/p>\n\n\n\n<p><strong>That is when we use<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\na = BookModel.objects.get(title = &quot;Ghostbuster&quot;)\n<\/pre><\/div>\n\n\n<p>Then all the information is stored as an object, so if we implement the following then it will print the corresponding values.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\na.title()\na.price()\na.genre()\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">5. Delete an row element from DB<\/h3>\n\n\n\n<p>To delete a particular element, we use the syntax <strong>.delete()<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: powershell; title: ; notranslate\" title=\"\">\nfrom books.models import BookModel\na =BookModel.objects.get(title=&quot;&lt;book_name&quot;&gt;)\na.delete()\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>Connecting to model DB via views.py<\/strong><\/h2>\n\n\n\n<p>Now we will learn how to take up information from the DB and then show it on our webpage.<\/p>\n\n\n\n<p>In <a href=\"https:\/\/www.askpython.com\/django\/django-views\" class=\"rank-math-link\">Views.py<\/a> add the code:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\ndef BookView(request):\n    books = BookModel.objects.all()\n\n    html = &#039;&#039;\n    for book in books:\n        var = f&#039;&lt;li&gt; {book.title} &lt;\/li&gt;&lt;br&gt;&#039;\n        html = html + var\n    return HttpResponse(html,status = 200)\n<\/pre><\/div>\n\n\n<p>Now from the code, u can understand that we basically retrieved all the DB info into variable <strong>books<\/strong> and then <strong>we started a loop to get each element <\/strong>from the <strong>DB<\/strong> and show on the webpage as <strong>HTML.<\/strong><\/p>\n\n\n\n<p>We must also provide the endpoint (<strong>Books\/<\/strong>) for this View. <strong>Try it<\/strong> on your own from the knowledge gained from the Django-URL mapping article<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"959\" height=\"217\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/urlbooks.png\" alt=\"URL mapping\" class=\"wp-image-7183\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/urlbooks.png 959w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/urlbooks-300x68.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/urlbooks-768x174.png 768w\" sizes=\"auto, (max-width: 959px) 100vw, 959px\" \/><figcaption>URL mapping<\/figcaption><\/figure><\/div>\n\n\n\n<p>Now for the web page <strong>(books\/&lt;Title_name&gt;)<\/strong>. Look at the code below to understand better. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"965\" height=\"357\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/BookIDView.png\" alt=\"BookIDView\" class=\"wp-image-7184\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/BookIDView.png 965w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/BookIDView-300x111.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/BookIDView-768x284.png 768w\" sizes=\"auto, (max-width: 965px) 100vw, 965px\" \/><figcaption>BookIDView<\/figcaption><\/figure><\/div>\n\n\n\n<p>The code is simple; we are just taking all the information about the <strong>book_name<\/strong> using <strong>Get<\/strong> and then showing it on the webpage as <strong>HTML<\/strong><\/p>\n\n\n\n<p>Here we have retrieved specific information from the DB and then displaying it on the Web page. Let us see how the urls.py file looks like for this View.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"968\" height=\"230\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/urlsbookid-1.png\" alt=\"URL mapping - BookIDView\" class=\"wp-image-7185\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/urlsbookid-1.png 968w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/urlsbookid-1-300x71.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/urlsbookid-1-768x182.png 768w\" sizes=\"auto, (max-width: 968px) 100vw, 968px\" \/><figcaption>URL mapping &#8211; BookIDView<\/figcaption><\/figure><\/div>\n\n\n\n<p>Now we will run the server and see if its working<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"725\" height=\"276\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/runserver-5.png\" alt=\"Runserver \" class=\"wp-image-7186\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/runserver-5.png 725w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/runserver-5-300x114.png 300w\" sizes=\"auto, (max-width: 725px) 100vw, 725px\" \/><figcaption>Runserver <\/figcaption><\/figure><\/div>\n\n\n\n<p>Browser page for<strong> books\/<\/strong> webpage<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"321\" height=\"165\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Browserbooks-1024x583-1.png\" alt=\"Browserbooks 1024x583 1\" class=\"wp-image-7316\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Browserbooks-1024x583-1.png 321w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Browserbooks-1024x583-1-300x154.png 300w\" sizes=\"auto, (max-width: 321px) 100vw, 321px\" \/><figcaption>Browser (books\/)<\/figcaption><\/figure><\/div>\n\n\n\n<p>Browser page for <strong>books\/&lt;Title_name&gt;<\/strong> webpage<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"325\" height=\"173\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/BrowserGhostbuster-1024x583-1.png\" alt=\"Browser Ghostbuster\" class=\"wp-image-7317\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/BrowserGhostbuster-1024x583-1.png 325w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/BrowserGhostbuster-1024x583-1-300x160.png 300w\" sizes=\"auto, (max-width: 325px) 100vw, 325px\" \/><figcaption>Browser <strong>(books\/Ghostbuster)<\/strong> <\/figcaption><\/figure><\/div>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"329\" height=\"204\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/BrowserHarry-Potter-1024x583-1.png\" alt=\"BrowserHarry Potter 1024x583 1\" class=\"wp-image-7318\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/BrowserHarry-Potter-1024x583-1.png 329w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/BrowserHarry-Potter-1024x583-1-300x186.png 300w\" sizes=\"auto, (max-width: 329px) 100vw, 329px\" \/><figcaption>Browser<strong> (books\/Harry Potter)<\/strong><\/figcaption><\/figure><\/div>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Connecting to other models <\/strong>using Foreign Key<\/h2>\n\n\n\n<p>A <strong>foreign key<\/strong>(FK) is used to <strong>link up two databases<\/strong> that are using some common information. This helps to keep our <strong>Databases clean <\/strong>and also ensures we don&#8217;t have to enter the same information again and again. <\/p>\n\n\n\n<p>Now in our books app, let us make an <strong>Author Model<\/strong> Table and also add the Author_id field in our Book Model. Now note that Several books might have the same Author, so <strong>Author_id will act as the Foreign Key in our BookModel.<\/strong> <\/p>\n\n\n\n<p>Using FK, we can search for various books written by a particular author. Also if an Author deletes his account, then all his books will also be deleted automatically thereby reducing the work of deleting them manually from the BookModel.<\/p>\n\n\n\n<p>You might get confused a bit in the starting but after repeated practice, it will start making sense. So <strong>Don&#8217;t Worry !!<\/strong><\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"953\" height=\"223\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/AuthorModel.png\" alt=\"AuthorModel\" class=\"wp-image-7190\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/AuthorModel.png 953w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/AuthorModel-300x70.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/AuthorModel-768x180.png 768w\" sizes=\"auto, (max-width: 953px) 100vw, 953px\" \/><figcaption>AuthorModel<\/figcaption><\/figure><\/div>\n\n\n\n<p>Now we can add the Author field in the <strong>BookModel.<\/strong> <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"964\" height=\"579\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Authorfield.png\" alt=\"Authorfield django models\" class=\"wp-image-7193\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Authorfield.png 964w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Authorfield-300x180.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Authorfield-768x461.png 768w\" sizes=\"auto, (max-width: 964px) 100vw, 964px\" \/><figcaption>Author_id Field<\/figcaption><\/figure><\/div>\n\n\n\n<p>We have to run migrations for our new AuthorModel. Also Note: we have to run the shellcodes<strong>(migrations)<\/strong> as mentioned earlier since we changed our <strong>BookModel<\/strong> DB.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"287\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/migrationsagain-1024x287.png\" alt=\"Migrations\" class=\"wp-image-7192\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/migrationsagain-1024x287.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/migrationsagain-300x84.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/migrationsagain-768x215.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/migrationsagain.png 1487w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Migrations<\/figcaption><\/figure>\n\n\n\n<p>We already had some information in the database without the <strong>Author field<\/strong>. So Django asks to enter a <strong>default value<\/strong> in the author_id field for them.<\/p>\n\n\n\n<p>It is preferred to delete all the earlier elements before applying the migrations <strong>(to change the table)<\/strong> using <strong>.delete()<\/strong><\/p>\n\n\n\n<p>We can now add information to Author DB in the same way we added Book Information. <\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"726\" height=\"201\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Author-DB.png\" alt=\"Author DB\" class=\"wp-image-7246\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Author-DB.png 726w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/07\/Author-DB-300x83.png 300w\" sizes=\"auto, (max-width: 726px) 100vw, 726px\" \/><figcaption>Author DB<\/figcaption><\/figure><\/div>\n\n\n\n<p>Also as practice, try to create an <strong>Author View <\/strong>yourself similar to the BookView.   <\/p>\n\n\n\n<p>We have now learnt to enter data, into tables through <strong>shell<\/strong>, but this is not the most efficient way to add data. The easiest way is to add data through <strong>admin site<\/strong>. We will learn how to do that in the next article.<\/p>\n\n\n\n<p>Also from the admin site, you will get more information about the working of this whole <strong>FK<\/strong>, and how Author DB and Model DB are linked with each other through <strong>FK.<\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>And thats it, we have reached the end of the article.  In the next article, we will learn about the <strong>admin site interface<\/strong> and then see the utility of <strong>FK<\/strong> in a better way. <strong>Keep coding !!<\/strong> <\/p>\n","protected":false},"excerpt":{"rendered":"<p>Django models are classes that represent a table or collection in our Database. It contains all the information regarding the table. These models are stored together in Django in a file models.py in our Django App. There can be many different models for different DB containing different information like User DB, Book DB, or any [&hellip;]<\/p>\n","protected":false},"author":13,"featured_media":7249,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[],"class_list":["post-7148","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-django"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7148","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=7148"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/7148\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/7249"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=7148"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=7148"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=7148"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}