{"title":"Writing your first Django app, part 2","version":"1.11","locale":"en","docname":"intro/tutorial02","url":"/en/1.11/intro/tutorial02/","canonical":"https://djangodocs.dev/en/1.11/intro/tutorial02/","summary":"This tutorial begins where Tutorial 1 left off. We’ll setup the database, create your first model, and get a quick introduction to Django’s automatically-generated…","html":"<h1>Writing your first Django app, part 2<a class=\"heading-anchor\" href=\"#writing-your-first-django-app-part-2\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h1>\n<p>This tutorial begins where <a class=\"reference internal\" href=\"/en/1.11/intro/tutorial01/\"><span class=\"doc\">Tutorial 1</span></a> left off.\nWe’ll setup the database, create your first model, and get a quick introduction\nto Django’s automatically-generated admin site.</p>\n<section id=\"database-setup\">\n<h2>Database setup<a class=\"heading-anchor\" href=\"#database-setup\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Now, open up <code class=\"file docutils literal notranslate\"><span class=\"pre\">mysite/settings.py</span></code>. It’s a normal Python module with\nmodule-level variables representing Django settings.</p>\n<p>By default, the configuration uses SQLite. If you’re new to databases, or\nyou’re just interested in trying Django, this is the easiest choice. SQLite is\nincluded in Python, so you won’t need to install anything else to support your\ndatabase. When starting your first real project, however, you may want to use a\nmore scalable database like PostgreSQL, to avoid database-switching headaches\ndown the road.</p>\n<p>If you wish to use another database, install the appropriate <a class=\"reference internal\" href=\"/en/1.11/topics/install/#database-installation\"><span class=\"std std-ref\">database\nbindings</span></a> and change the following keys in the\n<a class=\"reference internal\" href=\"/en/1.11/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DATABASES</span></code></a> <code class=\"docutils literal notranslate\"><span class=\"pre\">'default'</span></code> item to match your database connection\nsettings:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/en/1.11/ref/settings/#std-setting-DATABASE-ENGINE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">ENGINE</span></code></a> – Either\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'django.db.backends.sqlite3'</span></code>,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'django.db.backends.postgresql'</span></code>,\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'django.db.backends.mysql'</span></code>, or\n<code class=\"docutils literal notranslate\"><span class=\"pre\">'django.db.backends.oracle'</span></code>. Other backends are <a class=\"reference internal\" href=\"/en/1.11/ref/databases/#third-party-notes\"><span class=\"std std-ref\">also available</span></a>.</p></li>\n<li><p><a class=\"reference internal\" href=\"/en/1.11/ref/settings/#std-setting-NAME\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">NAME</span></code></a> – The name of your database. If you’re using SQLite, the\ndatabase will be a file on your computer; in that case, <a class=\"reference internal\" href=\"/en/1.11/ref/settings/#std-setting-NAME\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">NAME</span></code></a>\nshould be the full absolute path, including filename, of that file. The\ndefault value, <code class=\"docutils literal notranslate\"><span class=\"pre\">os.path.join(BASE_DIR,</span> <span class=\"pre\">'db.sqlite3')</span></code>, will store the file\nin your project directory.</p></li>\n</ul>\n<p>If you are not using SQLite as your database, additional settings such as\n<a class=\"reference internal\" href=\"/en/1.11/ref/settings/#std-setting-USER\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">USER</span></code></a>, <a class=\"reference internal\" href=\"/en/1.11/ref/settings/#std-setting-PASSWORD\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">PASSWORD</span></code></a>, and <a class=\"reference internal\" href=\"/en/1.11/ref/settings/#std-setting-HOST\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">HOST</span></code></a> must be added.\nFor more details, see the reference documentation for <a class=\"reference internal\" href=\"/en/1.11/ref/settings/#std-setting-DATABASES\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">DATABASES</span></code></a>.</p>\n<aside class=\"admonition-for-databases-other-than-sqlite admonition\">\n<p class=\"admonition-title\">For databases other than SQLite</p>\n<p>If you’re using a database besides SQLite, make sure you’ve created a\ndatabase by this point. Do that with “<code class=\"docutils literal notranslate\"><span class=\"pre\">CREATE</span> <span class=\"pre\">DATABASE</span> <span class=\"pre\">database_name;</span></code>”\nwithin your database’s interactive prompt.</p>\n<p>Also make sure that the database user provided in <code class=\"file docutils literal notranslate\"><span class=\"pre\">mysite/settings.py</span></code>\nhas “create database” privileges. This allows automatic creation of a\n<a class=\"reference internal\" href=\"/en/1.11/topics/testing/overview/#the-test-database\"><span class=\"std std-ref\">test database</span></a> which will be needed in a later\ntutorial.</p>\n<p>If you’re using SQLite, you don’t need to create anything beforehand - the\ndatabase file will be created automatically when it is needed.</p>\n</aside>\n<p>While you’re editing <code class=\"file docutils literal notranslate\"><span class=\"pre\">mysite/settings.py</span></code>, set <a class=\"reference internal\" href=\"/en/1.11/ref/settings/#std-setting-TIME_ZONE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TIME_ZONE</span></code></a> to\nyour time zone.</p>\n<p>Also, note the <a class=\"reference internal\" href=\"/en/1.11/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a> setting at the top of the file. That\nholds the names of all Django applications that are activated in this Django\ninstance. Apps can be used in multiple projects, and you can package and\ndistribute them for use by others in their projects.</p>\n<p>By default, <a class=\"reference internal\" href=\"/en/1.11/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a> contains the following apps, all of which\ncome with Django:</p>\n<ul class=\"simple\">\n<li><p><a class=\"reference internal\" href=\"/en/1.11/ref/contrib/admin/#module-django.contrib.admin\" title=\"django.contrib.admin: Django's admin site.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.admin</span></code></a> – The admin site. You’ll use it shortly.</p></li>\n<li><p><a class=\"reference internal\" href=\"/en/1.11/topics/auth/#module-django.contrib.auth\" title=\"django.contrib.auth: Django's authentication framework.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.auth</span></code></a> – An authentication system.</p></li>\n<li><p><a class=\"reference internal\" href=\"/en/1.11/ref/contrib/contenttypes/#module-django.contrib.contenttypes\" title=\"django.contrib.contenttypes: Provides generic interface to installed models.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.contenttypes</span></code></a> – A framework for content types.</p></li>\n<li><p><a class=\"reference internal\" href=\"/en/1.11/topics/http/sessions/#module-django.contrib.sessions\" title=\"django.contrib.sessions: Provides session management for Django projects.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.sessions</span></code></a> – A session framework.</p></li>\n<li><p><a class=\"reference internal\" href=\"/en/1.11/ref/contrib/messages/#module-django.contrib.messages\" title=\"django.contrib.messages: Provides cookie- and session-based temporary message storage.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.messages</span></code></a> – A messaging framework.</p></li>\n<li><p><a class=\"reference internal\" href=\"/en/1.11/ref/contrib/staticfiles/#module-django.contrib.staticfiles\" title=\"django.contrib.staticfiles: An app for handling static files.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.staticfiles</span></code></a> – A framework for managing\nstatic files.</p></li>\n</ul>\n<p>These applications are included by default as a convenience for the common case.</p>\n<p>Some of these applications make use of at least one database table, though,\nso we need to create the tables in the database before we can use them. To do\nthat, run the following command:</p>\n<div class=\"code-block\" data-language=\"console\"><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=\"gp\">$ </span>python<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>migrate\n</code></pre></div>\n<p>The <a class=\"reference internal\" href=\"/en/1.11/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">migrate</span></code></a> command looks at the <a class=\"reference internal\" href=\"/en/1.11/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a> setting\nand creates any necessary database tables according to the database settings\nin your <code class=\"file docutils literal notranslate\"><span class=\"pre\">mysite/settings.py</span></code> file and the database migrations shipped\nwith the app (we’ll cover those later). You’ll see a message for each\nmigration it applies. If you’re interested, run the command-line client for your\ndatabase and type <code class=\"docutils literal notranslate\"><span class=\"pre\">\\dt</span></code> (PostgreSQL), <code class=\"docutils literal notranslate\"><span class=\"pre\">SHOW</span> <span class=\"pre\">TABLES;</span></code> (MySQL), <code class=\"docutils literal notranslate\"><span class=\"pre\">.schema</span></code>\n(SQLite), or <code class=\"docutils literal notranslate\"><span class=\"pre\">SELECT</span> <span class=\"pre\">TABLE_NAME</span> <span class=\"pre\">FROM</span> <span class=\"pre\">USER_TABLES;</span></code> (Oracle) to display the\ntables Django created.</p>\n<aside class=\"admonition-for-the-minimalists admonition\">\n<p class=\"admonition-title\">For the minimalists</p>\n<p>Like we said above, the default applications are included for the common\ncase, but not everybody needs them. If you don’t need any or all of them,\nfeel free to comment-out or delete the appropriate line(s) from\n<a class=\"reference internal\" href=\"/en/1.11/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a> before running <a class=\"reference internal\" href=\"/en/1.11/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">migrate</span></code></a>. The\n<a class=\"reference internal\" href=\"/en/1.11/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">migrate</span></code></a> command will only run migrations for apps in\n<a class=\"reference internal\" href=\"/en/1.11/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a>.</p>\n</aside>\n</section>\n<section id=\"creating-models\">\n<span id=\"id1\"></span><h2>Creating models<a class=\"heading-anchor\" href=\"#creating-models\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Now we’ll define your models – essentially, your database layout, with\nadditional metadata.</p>\n<aside class=\"admonition-philosophy admonition\">\n<p class=\"admonition-title\">Philosophy</p>\n<p>A model is the single, definitive source of truth about your data. It contains\nthe essential fields and behaviors of the data you’re storing. Django follows\nthe <a class=\"reference internal\" href=\"/en/1.11/misc/design-philosophies/#dry\"><span class=\"std std-ref\">DRY Principle</span></a>. The goal is to define your data model in one\nplace and automatically derive things from it.</p>\n<p>This includes the migrations - unlike in Ruby On Rails, for example, migrations\nare entirely derived from your models file, and are essentially just a\nhistory that Django can roll through to update your database schema to\nmatch your current models.</p>\n</aside>\n<p>In our simple poll app, we’ll create two models: <code class=\"docutils literal notranslate\"><span class=\"pre\">Question</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">Choice</span></code>.\nA <code class=\"docutils literal notranslate\"><span class=\"pre\">Question</span></code> has a question and a publication date. A <code class=\"docutils literal notranslate\"><span class=\"pre\">Choice</span></code> has two\nfields: the text of the choice and a vote tally. Each <code class=\"docutils literal notranslate\"><span class=\"pre\">Choice</span></code> is associated\nwith a <code class=\"docutils literal notranslate\"><span class=\"pre\">Question</span></code>.</p>\n<p>These concepts are represented by simple Python classes. Edit the\n<code class=\"file docutils literal notranslate\"><span class=\"pre\">polls/models.py</span></code> file so it looks like this:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>polls/models.py</code></figcaption><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=\"polls/models.py\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.db</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">models</span>\n\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Question</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=\"n\">question_text</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\">200</span><span class=\"p\">)</span>\n    <span class=\"n\">pub_date</span> <span class=\"o\">=</span> <span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">DateTimeField</span><span class=\"p\">(</span><span class=\"s1\">&#39;date published&#39;</span><span class=\"p\">)</span>\n\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Choice</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=\"n\">question</span> <span class=\"o\">=</span> <span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">ForeignKey</span><span class=\"p\">(</span><span class=\"n\">Question</span><span class=\"p\">,</span> <span class=\"n\">on_delete</span><span class=\"o\">=</span><span class=\"n\">models</span><span class=\"o\">.</span><span class=\"n\">CASCADE</span><span class=\"p\">)</span>\n    <span class=\"n\">choice_text</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\">200</span><span class=\"p\">)</span>\n    <span class=\"n\">votes</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\">default</span><span class=\"o\">=</span><span class=\"mi\">0</span><span class=\"p\">)</span>\n</code></pre></figure>\n<p>The code is straightforward. Each model is represented by a class that\nsubclasses <a class=\"reference internal\" href=\"/en/1.11/ref/models/instances/#django.db.models.Model\" title=\"django.db.models.Model\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">django.db.models.Model</span></code></a>. Each model has a number of class\nvariables, each of which represents a database field in the model.</p>\n<p>Each field is represented by an instance of a <a class=\"reference internal\" href=\"/en/1.11/ref/models/fields/#django.db.models.Field\" title=\"django.db.models.Field\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Field</span></code></a>\nclass – e.g., <a class=\"reference internal\" href=\"/en/1.11/ref/models/fields/#django.db.models.CharField\" title=\"django.db.models.CharField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CharField</span></code></a> for character fields and\n<a class=\"reference internal\" href=\"/en/1.11/ref/models/fields/#django.db.models.DateTimeField\" title=\"django.db.models.DateTimeField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">DateTimeField</span></code></a> for datetimes. This tells Django what\ntype of data each field holds.</p>\n<p>The name of each <a class=\"reference internal\" href=\"/en/1.11/ref/models/fields/#django.db.models.Field\" title=\"django.db.models.Field\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Field</span></code></a> instance (e.g.\n<code class=\"docutils literal notranslate\"><span class=\"pre\">question_text</span></code> or <code class=\"docutils literal notranslate\"><span class=\"pre\">pub_date</span></code>) is the field’s name, in machine-friendly\nformat. You’ll use this value in your Python code, and your database will use\nit as the column name.</p>\n<p>You can use an optional first positional argument to a\n<a class=\"reference internal\" href=\"/en/1.11/ref/models/fields/#django.db.models.Field\" title=\"django.db.models.Field\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Field</span></code></a> to designate a human-readable name. That’s used\nin a couple of introspective parts of Django, and it doubles as documentation.\nIf this field isn’t provided, Django will use the machine-readable name. In this\nexample, we’ve only defined a human-readable name for <code class=\"docutils literal notranslate\"><span class=\"pre\">Question.pub_date</span></code>.\nFor all other fields in this model, the field’s machine-readable name will\nsuffice as its human-readable name.</p>\n<p>Some <a class=\"reference internal\" href=\"/en/1.11/ref/models/fields/#django.db.models.Field\" title=\"django.db.models.Field\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Field</span></code></a> classes have required arguments.\n<a class=\"reference internal\" href=\"/en/1.11/ref/models/fields/#django.db.models.CharField\" title=\"django.db.models.CharField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CharField</span></code></a>, for example, requires that you give it a\n<a class=\"reference internal\" href=\"/en/1.11/ref/models/fields/#django.db.models.CharField.max_length\" title=\"django.db.models.CharField.max_length\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">max_length</span></code></a>. That’s used not only in the\ndatabase schema, but in validation, as we’ll soon see.</p>\n<p>A <a class=\"reference internal\" href=\"/en/1.11/ref/models/fields/#django.db.models.Field\" title=\"django.db.models.Field\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">Field</span></code></a> can also have various optional arguments; in\nthis case, we’ve set the <a class=\"reference internal\" href=\"/en/1.11/ref/models/fields/#django.db.models.Field.default\" title=\"django.db.models.Field.default\"><code class=\"xref py py-attr docutils literal notranslate\"><span class=\"pre\">default</span></code></a> value of\n<code class=\"docutils literal notranslate\"><span class=\"pre\">votes</span></code> to 0.</p>\n<p>Finally, note a relationship is defined, using\n<a class=\"reference internal\" href=\"/en/1.11/ref/models/fields/#django.db.models.ForeignKey\" title=\"django.db.models.ForeignKey\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">ForeignKey</span></code></a>. That tells Django each <code class=\"docutils literal notranslate\"><span class=\"pre\">Choice</span></code> is\nrelated to a single <code class=\"docutils literal notranslate\"><span class=\"pre\">Question</span></code>. Django supports all the common database\nrelationships: many-to-one, many-to-many, and one-to-one.</p>\n</section>\n<section id=\"activating-models\">\n<h2>Activating models<a class=\"heading-anchor\" href=\"#activating-models\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>That small bit of model code gives Django a lot of information. With it, Django\nis able to:</p>\n<ul class=\"simple\">\n<li><p>Create a database schema (<code class=\"docutils literal notranslate\"><span class=\"pre\">CREATE</span> <span class=\"pre\">TABLE</span></code> statements) for this app.</p></li>\n<li><p>Create a Python database-access API for accessing <code class=\"docutils literal notranslate\"><span class=\"pre\">Question</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">Choice</span></code> objects.</p></li>\n</ul>\n<p>But first we need to tell our project that the <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code> app is installed.</p>\n<aside class=\"admonition-philosophy admonition\">\n<p class=\"admonition-title\">Philosophy</p>\n<p>Django apps are “pluggable”: You can use an app in multiple projects, and\nyou can distribute apps, because they don’t have to be tied to a given\nDjango installation.</p>\n</aside>\n<p>To include the app in our project, we need to add a reference to its\nconfiguration class in the <a class=\"reference internal\" href=\"/en/1.11/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a> setting. The\n<code class=\"docutils literal notranslate\"><span class=\"pre\">PollsConfig</span></code> class is in the <code class=\"file docutils literal notranslate\"><span class=\"pre\">polls/apps.py</span></code> file, so its dotted path\nis <code class=\"docutils literal notranslate\"><span class=\"pre\">'polls.apps.PollsConfig'</span></code>. Edit the <code class=\"file docutils literal notranslate\"><span class=\"pre\">mysite/settings.py</span></code> file and\nadd that dotted path to the <a class=\"reference internal\" href=\"/en/1.11/ref/settings/#std-setting-INSTALLED_APPS\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">INSTALLED_APPS</span></code></a> setting. It’ll look like\nthis:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>mysite/settings.py</code></figcaption><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=\"mysite/settings.py\"><code><span class=\"n\">INSTALLED_APPS</span> <span class=\"o\">=</span> <span class=\"p\">[</span>\n    <span class=\"s1\">&#39;polls.apps.PollsConfig&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;django.contrib.admin&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;django.contrib.auth&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;django.contrib.contenttypes&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;django.contrib.sessions&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;django.contrib.messages&#39;</span><span class=\"p\">,</span>\n    <span class=\"s1\">&#39;django.contrib.staticfiles&#39;</span><span class=\"p\">,</span>\n<span class=\"p\">]</span>\n</code></pre></figure>\n<p>Now Django knows to include the <code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code> app. Let’s run another command:</p>\n<div class=\"code-block\" data-language=\"console\"><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=\"gp\">$ </span>python<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>makemigrations<span class=\"w\"> </span>polls\n</code></pre></div>\n<p>You should see something similar to the following:</p>\n<div class=\"code-block\" data-language=\"text\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Text</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=\"Text code\"><code>Migrations for &#39;polls&#39;:\n  polls/migrations/0001_initial.py:\n    - Create model Choice\n    - Create model Question\n    - Add field question to choice\n</code></pre></div>\n<p>By running <code class=\"docutils literal notranslate\"><span class=\"pre\">makemigrations</span></code>, you’re telling Django that you’ve made\nsome changes to your models (in this case, you’ve made new ones) and that\nyou’d like the changes to be stored as a <em>migration</em>.</p>\n<p>Migrations are how Django stores changes to your models (and thus your\ndatabase schema) - they’re just files on disk. You can read the migration\nfor your new model if you like; it’s the file\n<code class=\"docutils literal notranslate\"><span class=\"pre\">polls/migrations/0001_initial.py</span></code>. Don’t worry, you’re not expected to read\nthem every time Django makes one, but they’re designed to be human-editable\nin case you want to manually tweak how Django changes things.</p>\n<p>There’s a command that will run the migrations for you and manage your database\nschema automatically - that’s called <a class=\"reference internal\" href=\"/en/1.11/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">migrate</span></code></a>, and we’ll come to it in a\nmoment - but first, let’s see what SQL that migration would run. The\n<a class=\"reference internal\" href=\"/en/1.11/ref/django-admin/#django-admin-sqlmigrate\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">sqlmigrate</span></code></a> command takes migration names and returns their SQL:</p>\n<div class=\"code-block\" data-language=\"console\"><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=\"gp\">$ </span>python<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>sqlmigrate<span class=\"w\"> </span>polls<span class=\"w\"> </span><span class=\"m\">0001</span>\n</code></pre></div>\n<p>You should see something similar to the following (we’ve reformatted it for\nreadability):</p>\n<div class=\"code-block\" data-language=\"sql\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">SQL</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=\"SQL code\"><code><span class=\"k\">BEGIN</span><span class=\"p\">;</span>\n<span class=\"c1\">--</span>\n<span class=\"c1\">-- Create model Choice</span>\n<span class=\"c1\">--</span>\n<span class=\"k\">CREATE</span><span class=\"w\"> </span><span class=\"k\">TABLE</span><span class=\"w\"> </span><span class=\"ss\">&quot;polls_choice&quot;</span><span class=\"w\"> </span><span class=\"p\">(</span>\n<span class=\"w\">    </span><span class=\"ss\">&quot;id&quot;</span><span class=\"w\"> </span><span class=\"nb\">serial</span><span class=\"w\"> </span><span class=\"k\">NOT</span><span class=\"w\"> </span><span class=\"k\">NULL</span><span class=\"w\"> </span><span class=\"k\">PRIMARY</span><span class=\"w\"> </span><span class=\"k\">KEY</span><span class=\"p\">,</span>\n<span class=\"w\">    </span><span class=\"ss\">&quot;choice_text&quot;</span><span class=\"w\"> </span><span class=\"nb\">varchar</span><span class=\"p\">(</span><span class=\"mi\">200</span><span class=\"p\">)</span><span class=\"w\"> </span><span class=\"k\">NOT</span><span class=\"w\"> </span><span class=\"k\">NULL</span><span class=\"p\">,</span>\n<span class=\"w\">    </span><span class=\"ss\">&quot;votes&quot;</span><span class=\"w\"> </span><span class=\"nb\">integer</span><span class=\"w\"> </span><span class=\"k\">NOT</span><span class=\"w\"> </span><span class=\"k\">NULL</span>\n<span class=\"p\">);</span>\n<span class=\"c1\">--</span>\n<span class=\"c1\">-- Create model Question</span>\n<span class=\"c1\">--</span>\n<span class=\"k\">CREATE</span><span class=\"w\"> </span><span class=\"k\">TABLE</span><span class=\"w\"> </span><span class=\"ss\">&quot;polls_question&quot;</span><span class=\"w\"> </span><span class=\"p\">(</span>\n<span class=\"w\">    </span><span class=\"ss\">&quot;id&quot;</span><span class=\"w\"> </span><span class=\"nb\">serial</span><span class=\"w\"> </span><span class=\"k\">NOT</span><span class=\"w\"> </span><span class=\"k\">NULL</span><span class=\"w\"> </span><span class=\"k\">PRIMARY</span><span class=\"w\"> </span><span class=\"k\">KEY</span><span class=\"p\">,</span>\n<span class=\"w\">    </span><span class=\"ss\">&quot;question_text&quot;</span><span class=\"w\"> </span><span class=\"nb\">varchar</span><span class=\"p\">(</span><span class=\"mi\">200</span><span class=\"p\">)</span><span class=\"w\"> </span><span class=\"k\">NOT</span><span class=\"w\"> </span><span class=\"k\">NULL</span><span class=\"p\">,</span>\n<span class=\"w\">    </span><span class=\"ss\">&quot;pub_date&quot;</span><span class=\"w\"> </span><span class=\"k\">timestamp</span><span class=\"w\"> </span><span class=\"k\">with</span><span class=\"w\"> </span><span class=\"k\">time</span><span class=\"w\"> </span><span class=\"k\">zone</span><span class=\"w\"> </span><span class=\"k\">NOT</span><span class=\"w\"> </span><span class=\"k\">NULL</span>\n<span class=\"p\">);</span>\n<span class=\"c1\">--</span>\n<span class=\"c1\">-- Add field question to choice</span>\n<span class=\"c1\">--</span>\n<span class=\"k\">ALTER</span><span class=\"w\"> </span><span class=\"k\">TABLE</span><span class=\"w\"> </span><span class=\"ss\">&quot;polls_choice&quot;</span><span class=\"w\"> </span><span class=\"k\">ADD</span><span class=\"w\"> </span><span class=\"k\">COLUMN</span><span class=\"w\"> </span><span class=\"ss\">&quot;question_id&quot;</span><span class=\"w\"> </span><span class=\"nb\">integer</span><span class=\"w\"> </span><span class=\"k\">NOT</span><span class=\"w\"> </span><span class=\"k\">NULL</span><span class=\"p\">;</span>\n<span class=\"k\">ALTER</span><span class=\"w\"> </span><span class=\"k\">TABLE</span><span class=\"w\"> </span><span class=\"ss\">&quot;polls_choice&quot;</span><span class=\"w\"> </span><span class=\"k\">ALTER</span><span class=\"w\"> </span><span class=\"k\">COLUMN</span><span class=\"w\"> </span><span class=\"ss\">&quot;question_id&quot;</span><span class=\"w\"> </span><span class=\"k\">DROP</span><span class=\"w\"> </span><span class=\"k\">DEFAULT</span><span class=\"p\">;</span>\n<span class=\"k\">CREATE</span><span class=\"w\"> </span><span class=\"k\">INDEX</span><span class=\"w\"> </span><span class=\"ss\">&quot;polls_choice_7aa0f6ee&quot;</span><span class=\"w\"> </span><span class=\"k\">ON</span><span class=\"w\"> </span><span class=\"ss\">&quot;polls_choice&quot;</span><span class=\"w\"> </span><span class=\"p\">(</span><span class=\"ss\">&quot;question_id&quot;</span><span class=\"p\">);</span>\n<span class=\"k\">ALTER</span><span class=\"w\"> </span><span class=\"k\">TABLE</span><span class=\"w\"> </span><span class=\"ss\">&quot;polls_choice&quot;</span>\n<span class=\"w\">  </span><span class=\"k\">ADD</span><span class=\"w\"> </span><span class=\"k\">CONSTRAINT</span><span class=\"w\"> </span><span class=\"ss\">&quot;polls_choice_question_id_246c99a640fbbd72_fk_polls_question_id&quot;</span>\n<span class=\"w\">    </span><span class=\"k\">FOREIGN</span><span class=\"w\"> </span><span class=\"k\">KEY</span><span class=\"w\"> </span><span class=\"p\">(</span><span class=\"ss\">&quot;question_id&quot;</span><span class=\"p\">)</span>\n<span class=\"w\">    </span><span class=\"k\">REFERENCES</span><span class=\"w\"> </span><span class=\"ss\">&quot;polls_question&quot;</span><span class=\"w\"> </span><span class=\"p\">(</span><span class=\"ss\">&quot;id&quot;</span><span class=\"p\">)</span>\n<span class=\"w\">    </span><span class=\"k\">DEFERRABLE</span><span class=\"w\"> </span><span class=\"k\">INITIALLY</span><span class=\"w\"> </span><span class=\"k\">DEFERRED</span><span class=\"p\">;</span>\n\n<span class=\"k\">COMMIT</span><span class=\"p\">;</span>\n</code></pre></div>\n<p>Note the following:</p>\n<ul class=\"simple\">\n<li><p>The exact output will vary depending on the database you are using. The\nexample above is generated for PostgreSQL.</p></li>\n<li><p>Table names are automatically generated by combining the name of the app\n(<code class=\"docutils literal notranslate\"><span class=\"pre\">polls</span></code>) and the lowercase name of the model – <code class=\"docutils literal notranslate\"><span class=\"pre\">question</span></code> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">choice</span></code>. (You can override this behavior.)</p></li>\n<li><p>Primary keys (IDs) are added automatically. (You can override this, too.)</p></li>\n<li><p>By convention, Django appends <code class=\"docutils literal notranslate\"><span class=\"pre\">&quot;_id&quot;</span></code> to the foreign key field name.\n(Yes, you can override this, as well.)</p></li>\n<li><p>The foreign key relationship is made explicit by a <code class=\"docutils literal notranslate\"><span class=\"pre\">FOREIGN</span> <span class=\"pre\">KEY</span></code>\nconstraint. Don’t worry about the <code class=\"docutils literal notranslate\"><span class=\"pre\">DEFERRABLE</span></code> parts; that’s just telling\nPostgreSQL to not enforce the foreign key until the end of the transaction.</p></li>\n<li><p>It’s tailored to the database you’re using, so database-specific field types\nsuch as <code class=\"docutils literal notranslate\"><span class=\"pre\">auto_increment</span></code> (MySQL), <code class=\"docutils literal notranslate\"><span class=\"pre\">serial</span></code> (PostgreSQL), or <code class=\"docutils literal notranslate\"><span class=\"pre\">integer</span>\n<span class=\"pre\">primary</span> <span class=\"pre\">key</span> <span class=\"pre\">autoincrement</span></code> (SQLite) are handled for you automatically. Same\ngoes for the quoting of field names – e.g., using double quotes or\nsingle quotes.</p></li>\n<li><p>The <a class=\"reference internal\" href=\"/en/1.11/ref/django-admin/#django-admin-sqlmigrate\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">sqlmigrate</span></code></a> command doesn’t actually run the migration on your\ndatabase - it just prints it to the screen so that you can see what SQL\nDjango thinks is required. It’s useful for checking what Django is going to\ndo or if you have database administrators who require SQL scripts for\nchanges.</p></li>\n</ul>\n<p>If you’re interested, you can also run\n<a class=\"reference internal\" href=\"/en/1.11/ref/django-admin/#django-admin-check\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">python</span> <span class=\"pre\">manage.py</span> <span class=\"pre\">check</span></code></a>; this checks for any problems in\nyour project without making migrations or touching the database.</p>\n<p>Now, run <a class=\"reference internal\" href=\"/en/1.11/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">migrate</span></code></a> again to create those model tables in your database:</p>\n<div class=\"code-block\" data-language=\"console\"><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=\"gp\">$ </span>python<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>migrate\n<span class=\"go\">Operations to perform:</span>\n<span class=\"go\">  Apply all migrations: admin, auth, contenttypes, polls, sessions</span>\n<span class=\"go\">Running migrations:</span>\n<span class=\"go\">  Rendering model states... DONE</span>\n<span class=\"go\">  Applying polls.0001_initial... OK</span>\n</code></pre></div>\n<p>The <a class=\"reference internal\" href=\"/en/1.11/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">migrate</span></code></a> command takes all the migrations that haven’t been\napplied (Django tracks which ones are applied using a special table in your\ndatabase called <code class=\"docutils literal notranslate\"><span class=\"pre\">django_migrations</span></code>) and runs them against your database -\nessentially, synchronizing the changes you made to your models with the schema\nin the database.</p>\n<p>Migrations are very powerful and let you change your models over time, as you\ndevelop your project, without the need to delete your database or tables and\nmake new ones - it specializes in upgrading your database live, without\nlosing data. We’ll cover them in more depth in a later part of the tutorial,\nbut for now, remember the three-step guide to making model changes:</p>\n<ul class=\"simple\">\n<li><p>Change your models (in <code class=\"docutils literal notranslate\"><span class=\"pre\">models.py</span></code>).</p></li>\n<li><p>Run <a class=\"reference internal\" href=\"/en/1.11/ref/django-admin/#django-admin-makemigrations\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">python</span> <span class=\"pre\">manage.py</span> <span class=\"pre\">makemigrations</span></code></a> to create\nmigrations for those changes</p></li>\n<li><p>Run <a class=\"reference internal\" href=\"/en/1.11/ref/django-admin/#django-admin-migrate\"><code class=\"xref std std-djadmin docutils literal notranslate\"><span class=\"pre\">python</span> <span class=\"pre\">manage.py</span> <span class=\"pre\">migrate</span></code></a> to apply those changes to\nthe database.</p></li>\n</ul>\n<p>The reason that there are separate commands to make and apply migrations is\nbecause you’ll commit migrations to your version control system and ship them\nwith your app; they not only make your development easier, they’re also\nuseable by other developers and in production.</p>\n<p>Read the <a class=\"reference internal\" href=\"/en/1.11/ref/django-admin/\"><span class=\"doc\">django-admin documentation</span></a> for full\ninformation on what the <code class=\"docutils literal notranslate\"><span class=\"pre\">manage.py</span></code> utility can do.</p>\n</section>\n<section id=\"playing-with-the-api\">\n<h2>Playing with the API<a class=\"heading-anchor\" href=\"#playing-with-the-api\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<p>Now, let’s hop into the interactive Python shell and play around with the free\nAPI Django gives you. To invoke the Python shell, use this command:</p>\n<div class=\"code-block\" data-language=\"console\"><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=\"gp\">$ </span>python<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>shell\n</code></pre></div>\n<p>We’re using this instead of simply typing “python”, because <code class=\"file docutils literal notranslate\"><span class=\"pre\">manage.py</span></code>\nsets the <code class=\"docutils literal notranslate\"><span class=\"pre\">DJANGO_SETTINGS_MODULE</span></code> environment variable, which gives Django\nthe Python import path to your <code class=\"file docutils literal notranslate\"><span class=\"pre\">mysite/settings.py</span></code> file.</p>\n<aside class=\"admonition-bypassing-manage-py admonition\">\n<p class=\"admonition-title\">Bypassing manage.py</p>\n<p>If you’d rather not use <code class=\"file docutils literal notranslate\"><span class=\"pre\">manage.py</span></code>, no problem. Just set the\n<span class=\"target\" id=\"index-0\"></span><a class=\"reference internal\" href=\"/en/1.11/topics/settings/#envvar-DJANGO_SETTINGS_MODULE\"><code class=\"xref std std-envvar docutils literal notranslate\"><span class=\"pre\">DJANGO_SETTINGS_MODULE</span></code></a> environment variable to\n<code class=\"docutils literal notranslate\"><span class=\"pre\">mysite.settings</span></code>, start a plain Python shell, and set up Django:</p>\n<div class=\"code-block\" data-language=\"pycon\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Python console</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=\"Python console code\"><code><span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">django</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">django</span><span class=\"o\">.</span><span class=\"n\">setup</span><span class=\"p\">()</span>\n</code></pre></div>\n<p>If this raises an <a class=\"reference external\" href=\"https://docs.python.org/3/library/exceptions.html#AttributeError\" title=\"(in Python v3.14)\"><code class=\"xref py py-exc docutils literal notranslate\"><span class=\"pre\">AttributeError</span></code></a>, you’re probably using\na version of Django that doesn’t match this tutorial version. You’ll want\nto either switch to the older tutorial or the newer Django version.</p>\n<p>You must run <code class=\"docutils literal notranslate\"><span class=\"pre\">python</span></code> from the same directory <code class=\"file docutils literal notranslate\"><span class=\"pre\">manage.py</span></code> is in,\nor ensure that directory is on the Python path, so that <code class=\"docutils literal notranslate\"><span class=\"pre\">import</span> <span class=\"pre\">mysite</span></code>\nworks.</p>\n<p>For more information on all of this, see the <a class=\"reference internal\" href=\"/en/1.11/ref/django-admin/\"><span class=\"doc\">django-admin\ndocumentation</span></a>.</p>\n</aside>\n<p>Once you’re in the shell, explore the <a class=\"reference internal\" href=\"/en/1.11/topics/db/queries/\"><span class=\"doc\">database API</span></a>:</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=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">polls.models</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Question</span><span class=\"p\">,</span> <span class=\"n\">Choice</span>   <span class=\"c1\"># Import the model classes we just wrote.</span>\n\n<span class=\"go\"># No questions are in the system yet.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">Question</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">all</span><span class=\"p\">()</span>\n<span class=\"go\">&lt;QuerySet []&gt;</span>\n\n<span class=\"go\"># Create a new Question.</span>\n<span class=\"go\"># Support for time zones is enabled in the default settings file, so</span>\n<span class=\"go\"># Django expects a datetime with tzinfo for pub_date. Use timezone.now()</span>\n<span class=\"go\"># instead of datetime.datetime.now() and it will do the right thing.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">timezone</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span> <span class=\"o\">=</span> <span class=\"n\">Question</span><span class=\"p\">(</span><span class=\"n\">question_text</span><span class=\"o\">=</span><span class=\"s2\">&quot;What&#39;s new?&quot;</span><span class=\"p\">,</span> <span class=\"n\">pub_date</span><span class=\"o\">=</span><span class=\"n\">timezone</span><span class=\"o\">.</span><span class=\"n\">now</span><span class=\"p\">())</span>\n\n<span class=\"go\"># Save the object into the database. You have to call save() explicitly.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">save</span><span class=\"p\">()</span>\n\n<span class=\"go\"># Now it has an ID. Note that this might say &quot;1L&quot; instead of &quot;1&quot;, depending</span>\n<span class=\"go\"># on which database you&#39;re using. That&#39;s no biggie; it just means your</span>\n<span class=\"go\"># database backend prefers to return integers as Python long integer</span>\n<span class=\"go\"># objects.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">id</span>\n<span class=\"go\">1</span>\n\n<span class=\"go\"># Access model field values via Python attributes.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">question_text</span>\n<span class=\"go\">&quot;What&#39;s new?&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">pub_date</span>\n<span class=\"go\">datetime.datetime(2012, 2, 26, 13, 0, 0, 775217, tzinfo=&lt;UTC&gt;)</span>\n\n<span class=\"go\"># Change values by changing the attributes, then calling save().</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">question_text</span> <span class=\"o\">=</span> <span class=\"s2\">&quot;What&#39;s up?&quot;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">save</span><span class=\"p\">()</span>\n\n<span class=\"go\"># objects.all() displays all the questions in the database.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">Question</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">all</span><span class=\"p\">()</span>\n<span class=\"go\">&lt;QuerySet [&lt;Question: Question object&gt;]&gt;</span>\n</code></pre></div>\n<p>Wait a minute. <code class=\"docutils literal notranslate\"><span class=\"pre\">&lt;Question:</span> <span class=\"pre\">Question</span> <span class=\"pre\">object&gt;</span></code> is, utterly, an unhelpful representation\nof this object. Let’s fix that by editing the <code class=\"docutils literal notranslate\"><span class=\"pre\">Question</span></code> model (in the\n<code class=\"docutils literal notranslate\"><span class=\"pre\">polls/models.py</span></code> file) and adding a\n<a class=\"reference internal\" href=\"/en/1.11/ref/models/instances/#django.db.models.Model.__str__\" title=\"django.db.models.Model.__str__\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">__str__()</span></code></a> method to both <code class=\"docutils literal notranslate\"><span class=\"pre\">Question</span></code> and\n<code class=\"docutils literal notranslate\"><span class=\"pre\">Choice</span></code>:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>polls/models.py</code></figcaption><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=\"polls/models.py\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.db</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">models</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils.encoding</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">python_2_unicode_compatible</span>\n\n<span class=\"nd\">@python_2_unicode_compatible</span>  <span class=\"c1\"># only if you need to support Python 2</span>\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Question</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=\"c1\"># ...</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"fm\">__str__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">question_text</span>\n\n<span class=\"nd\">@python_2_unicode_compatible</span>  <span class=\"c1\"># only if you need to support Python 2</span>\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Choice</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=\"c1\"># ...</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"fm\">__str__</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">choice_text</span>\n</code></pre></figure>\n<p>It’s important to add <a class=\"reference internal\" href=\"/en/1.11/ref/models/instances/#django.db.models.Model.__str__\" title=\"django.db.models.Model.__str__\"><code class=\"xref py py-meth docutils literal notranslate\"><span class=\"pre\">__str__()</span></code></a> methods to your\nmodels, not only for your own convenience when dealing with the interactive\nprompt, but also because objects’ representations are used throughout Django’s\nautomatically-generated admin.</p>\n<p>Note these are normal Python methods. Let’s add a custom method, just for\ndemonstration:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>polls/models.py</code></figcaption><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=\"polls/models.py\"><code><span class=\"kn\">import</span><span class=\"w\"> </span><span class=\"nn\">datetime</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.db</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">models</span>\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">timezone</span>\n\n\n<span class=\"k\">class</span><span class=\"w\"> </span><span class=\"nc\">Question</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=\"c1\"># ...</span>\n    <span class=\"k\">def</span><span class=\"w\"> </span><span class=\"nf\">was_published_recently</span><span class=\"p\">(</span><span class=\"bp\">self</span><span class=\"p\">):</span>\n        <span class=\"k\">return</span> <span class=\"bp\">self</span><span class=\"o\">.</span><span class=\"n\">pub_date</span> <span class=\"o\">&gt;=</span> <span class=\"n\">timezone</span><span class=\"o\">.</span><span class=\"n\">now</span><span class=\"p\">()</span> <span class=\"o\">-</span> <span class=\"n\">datetime</span><span class=\"o\">.</span><span class=\"n\">timedelta</span><span class=\"p\">(</span><span class=\"n\">days</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n</code></pre></figure>\n<p>Note the addition of <code class=\"docutils literal notranslate\"><span class=\"pre\">import</span> <span class=\"pre\">datetime</span></code> and <code class=\"docutils literal notranslate\"><span class=\"pre\">from</span> <span class=\"pre\">django.utils</span> <span class=\"pre\">import</span>\n<span class=\"pre\">timezone</span></code>, to reference Python’s standard <a class=\"reference external\" href=\"https://docs.python.org/3/library/datetime.html#module-datetime\" title=\"(in Python v3.14)\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">datetime</span></code></a> module and Django’s\ntime-zone-related utilities in <a class=\"reference internal\" href=\"/en/1.11/ref/utils/#module-django.utils.timezone\" title=\"django.utils.timezone: Timezone support.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.utils.timezone</span></code></a>, respectively. If\nyou aren’t familiar with time zone handling in Python, you can learn more in\nthe <a class=\"reference internal\" href=\"/en/1.11/topics/i18n/timezones/\"><span class=\"doc\">time zone support docs</span></a>.</p>\n<p>Save these changes and start a new Python interactive shell by running\n<code class=\"docutils literal notranslate\"><span class=\"pre\">python</span> <span class=\"pre\">manage.py</span> <span class=\"pre\">shell</span></code> again:</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=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">polls.models</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Question</span><span class=\"p\">,</span> <span class=\"n\">Choice</span>\n\n<span class=\"go\"># Make sure our __str__() addition worked.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">Question</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">all</span><span class=\"p\">()</span>\n<span class=\"go\">&lt;QuerySet [&lt;Question: What&#39;s up?&gt;]&gt;</span>\n\n<span class=\"go\"># Django provides a rich database lookup API that&#39;s entirely driven by</span>\n<span class=\"go\"># keyword arguments.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">Question</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"nb\">id</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;QuerySet [&lt;Question: What&#39;s up?&gt;]&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">Question</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"n\">question_text__startswith</span><span class=\"o\">=</span><span class=\"s1\">&#39;What&#39;</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;QuerySet [&lt;Question: What&#39;s up?&gt;]&gt;</span>\n\n<span class=\"go\"># Get the question that was published this year.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.utils</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">timezone</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">current_year</span> <span class=\"o\">=</span> <span class=\"n\">timezone</span><span class=\"o\">.</span><span class=\"n\">now</span><span class=\"p\">()</span><span class=\"o\">.</span><span class=\"n\">year</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">Question</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"n\">pub_date__year</span><span class=\"o\">=</span><span class=\"n\">current_year</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;Question: What&#39;s up?&gt;</span>\n\n<span class=\"go\"># Request an ID that doesn&#39;t exist, this will raise an exception.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">Question</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"nb\">id</span><span class=\"o\">=</span><span class=\"mi\">2</span><span class=\"p\">)</span>\n<span class=\"gt\">Traceback (most recent call last):</span>\n<span class=\"w\">    </span><span class=\"o\">...</span>\n<span class=\"gr\">DoesNotExist</span>: <span class=\"n\">Question matching query does not exist.</span>\n\n<span class=\"x\"># Lookup by a primary key is the most common case, so Django provides a</span>\n<span class=\"x\"># shortcut for primary-key exact lookups.</span>\n<span class=\"x\"># The following is identical to Question.objects.get(id=1).</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">Question</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"n\">pk</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;Question: What&#39;s up?&gt;</span>\n\n<span class=\"go\"># Make sure our custom method worked.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span> <span class=\"o\">=</span> <span class=\"n\">Question</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"n\">pk</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">was_published_recently</span><span class=\"p\">()</span>\n<span class=\"go\">True</span>\n\n<span class=\"go\"># Give the Question a couple of Choices. The create call constructs a new</span>\n<span class=\"go\"># Choice object, does the INSERT statement, adds the choice to the set</span>\n<span class=\"go\"># of available choices and returns the new Choice object. Django creates</span>\n<span class=\"go\"># a set to hold the &quot;other side&quot; of a ForeignKey relation</span>\n<span class=\"go\"># (e.g. a question&#39;s choice) which can be accessed via the API.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span> <span class=\"o\">=</span> <span class=\"n\">Question</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">get</span><span class=\"p\">(</span><span class=\"n\">pk</span><span class=\"o\">=</span><span class=\"mi\">1</span><span class=\"p\">)</span>\n\n<span class=\"go\"># Display any choices from the related object set -- none so far.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">choice_set</span><span class=\"o\">.</span><span class=\"n\">all</span><span class=\"p\">()</span>\n<span class=\"go\">&lt;QuerySet []&gt;</span>\n\n<span class=\"go\"># Create three choices.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">choice_set</span><span class=\"o\">.</span><span class=\"n\">create</span><span class=\"p\">(</span><span class=\"n\">choice_text</span><span class=\"o\">=</span><span class=\"s1\">&#39;Not much&#39;</span><span class=\"p\">,</span> <span class=\"n\">votes</span><span class=\"o\">=</span><span class=\"mi\">0</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;Choice: Not much&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">choice_set</span><span class=\"o\">.</span><span class=\"n\">create</span><span class=\"p\">(</span><span class=\"n\">choice_text</span><span class=\"o\">=</span><span class=\"s1\">&#39;The sky&#39;</span><span class=\"p\">,</span> <span class=\"n\">votes</span><span class=\"o\">=</span><span class=\"mi\">0</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;Choice: The sky&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">c</span> <span class=\"o\">=</span> <span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">choice_set</span><span class=\"o\">.</span><span class=\"n\">create</span><span class=\"p\">(</span><span class=\"n\">choice_text</span><span class=\"o\">=</span><span class=\"s1\">&#39;Just hacking again&#39;</span><span class=\"p\">,</span> <span class=\"n\">votes</span><span class=\"o\">=</span><span class=\"mi\">0</span><span class=\"p\">)</span>\n\n<span class=\"go\"># Choice objects have API access to their related Question objects.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">c</span><span class=\"o\">.</span><span class=\"n\">question</span>\n<span class=\"go\">&lt;Question: What&#39;s up?&gt;</span>\n\n<span class=\"go\"># And vice versa: Question objects get access to Choice objects.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">choice_set</span><span class=\"o\">.</span><span class=\"n\">all</span><span class=\"p\">()</span>\n<span class=\"go\">&lt;QuerySet [&lt;Choice: Not much&gt;, &lt;Choice: The sky&gt;, &lt;Choice: Just hacking again&gt;]&gt;</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">choice_set</span><span class=\"o\">.</span><span class=\"n\">count</span><span class=\"p\">()</span>\n<span class=\"go\">3</span>\n\n<span class=\"go\"># The API automatically follows relationships as far as you need.</span>\n<span class=\"go\"># Use double underscores to separate relationships.</span>\n<span class=\"go\"># This works as many levels deep as you want; there&#39;s no limit.</span>\n<span class=\"go\"># Find all Choices for any question whose pub_date is in this year</span>\n<span class=\"go\"># (reusing the &#39;current_year&#39; variable we created above).</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">Choice</span><span class=\"o\">.</span><span class=\"n\">objects</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"n\">question__pub_date__year</span><span class=\"o\">=</span><span class=\"n\">current_year</span><span class=\"p\">)</span>\n<span class=\"go\">&lt;QuerySet [&lt;Choice: Not much&gt;, &lt;Choice: The sky&gt;, &lt;Choice: Just hacking again&gt;]&gt;</span>\n\n<span class=\"go\"># Let&#39;s delete one of the choices. Use delete() for that.</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">c</span> <span class=\"o\">=</span> <span class=\"n\">q</span><span class=\"o\">.</span><span class=\"n\">choice_set</span><span class=\"o\">.</span><span class=\"n\">filter</span><span class=\"p\">(</span><span class=\"n\">choice_text__startswith</span><span class=\"o\">=</span><span class=\"s1\">&#39;Just hacking&#39;</span><span class=\"p\">)</span>\n<span class=\"gp\">&gt;&gt;&gt; </span><span class=\"n\">c</span><span class=\"o\">.</span><span class=\"n\">delete</span><span class=\"p\">()</span>\n</code></pre></div>\n<p>For more information on model relations, see <a class=\"reference internal\" href=\"/en/1.11/ref/models/relations/\"><span class=\"doc\">Accessing related objects</span></a>. For more on how to use double underscores to perform\nfield lookups via the API, see <a class=\"reference internal\" href=\"/en/1.11/topics/db/queries/#field-lookups-intro\"><span class=\"std std-ref\">Field lookups</span></a>. For\nfull details on the database API, see our <a class=\"reference internal\" href=\"/en/1.11/topics/db/queries/\"><span class=\"doc\">Database API reference</span></a>.</p>\n</section>\n<section id=\"introducing-the-django-admin\">\n<h2>Introducing the Django Admin<a class=\"heading-anchor\" href=\"#introducing-the-django-admin\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h2>\n<aside class=\"admonition-philosophy admonition\">\n<p class=\"admonition-title\">Philosophy</p>\n<p>Generating admin sites for your staff or clients to add, change, and delete\ncontent is tedious work that doesn’t require much creativity. For that\nreason, Django entirely automates creation of admin interfaces for models.</p>\n<p>Django was written in a newsroom environment, with a very clear separation\nbetween “content publishers” and the “public” site. Site managers use the\nsystem to add news stories, events, sports scores, etc., and that content is\ndisplayed on the public site. Django solves the problem of creating a\nunified interface for site administrators to edit content.</p>\n<p>The admin isn’t intended to be used by site visitors. It’s for site\nmanagers.</p>\n</aside>\n<section id=\"creating-an-admin-user\">\n<h3>Creating an admin user<a class=\"heading-anchor\" href=\"#creating-an-admin-user\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>First we’ll need to create a user who can login to the admin site. Run the\nfollowing command:</p>\n<div class=\"code-block\" data-language=\"console\"><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=\"gp\">$ </span>python<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>createsuperuser\n</code></pre></div>\n<p>Enter your desired username and press enter.</p>\n<div class=\"code-block\" data-language=\"text\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Text</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=\"Text code\"><code>Username: admin\n</code></pre></div>\n<p>You will then be prompted for your desired email address:</p>\n<div class=\"code-block\" data-language=\"text\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Text</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=\"Text code\"><code>Email address: admin@example.com\n</code></pre></div>\n<p>The final step is to enter your password. You will be asked to enter your\npassword twice, the second time as a confirmation of the first.</p>\n<div class=\"code-block\" data-language=\"text\"><div class=\"code-block-toolbar\"><span class=\"code-block-language\">Text</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=\"Text code\"><code>Password: **********\nPassword (again): *********\nSuperuser created successfully.\n</code></pre></div>\n</section>\n<section id=\"start-the-development-server\">\n<h3>Start the development server<a class=\"heading-anchor\" href=\"#start-the-development-server\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>The Django admin site is activated by default. Let’s start the development\nserver and explore it.</p>\n<p>If the server is not running start it like so:</p>\n<div class=\"code-block\" data-language=\"console\"><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=\"gp\">$ </span>python<span class=\"w\"> </span>manage.py<span class=\"w\"> </span>runserver\n</code></pre></div>\n<p>Now, open a Web browser and go to “/admin/” on your local domain – e.g.,\n<a class=\"reference external\" href=\"http://127.0.0.1:8000/admin/\">http://127.0.0.1:8000/admin/</a>. You should see the admin’s login screen:</p>\n<img alt=\"Django admin login screen\" src=\"/en/1.11/_images/admin01.png\" />\n<p>Since <a class=\"reference internal\" href=\"/en/1.11/topics/i18n/translation/\"><span class=\"doc\">translation</span></a> is turned on by default,\nthe login screen may be displayed in your own language, depending on your\nbrowser’s settings and if Django has a translation for this language.</p>\n</section>\n<section id=\"enter-the-admin-site\">\n<h3>Enter the admin site<a class=\"heading-anchor\" href=\"#enter-the-admin-site\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Now, try logging in with the superuser account you created in the previous step.\nYou should see the Django admin index page:</p>\n<img alt=\"Django admin index page\" src=\"/en/1.11/_images/admin02.png\" />\n<p>You should see a few types of editable content: groups and users. They are\nprovided by <a class=\"reference internal\" href=\"/en/1.11/topics/auth/#module-django.contrib.auth\" title=\"django.contrib.auth: Django's authentication framework.\"><code class=\"xref py py-mod docutils literal notranslate\"><span class=\"pre\">django.contrib.auth</span></code></a>, the authentication framework shipped\nby Django.</p>\n</section>\n<section id=\"make-the-poll-app-modifiable-in-the-admin\">\n<h3>Make the poll app modifiable in the admin<a class=\"heading-anchor\" href=\"#make-the-poll-app-modifiable-in-the-admin\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>But where’s our poll app? It’s not displayed on the admin index page.</p>\n<p>Just one thing to do: we need to tell the admin that <code class=\"docutils literal notranslate\"><span class=\"pre\">Question</span></code>\nobjects have an admin interface. To do this, open the <code class=\"file docutils literal notranslate\"><span class=\"pre\">polls/admin.py</span></code>\nfile, and edit it to look like this:</p>\n<figure class=\"code-block code-block-captioned\" data-language=\"default\"><figcaption class=\"code-block-caption\"><code>polls/admin.py</code></figcaption><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=\"polls/admin.py\"><code><span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">django.contrib</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">admin</span>\n\n<span class=\"kn\">from</span><span class=\"w\"> </span><span class=\"nn\">.models</span><span class=\"w\"> </span><span class=\"kn\">import</span> <span class=\"n\">Question</span>\n\n<span class=\"n\">admin</span><span class=\"o\">.</span><span class=\"n\">site</span><span class=\"o\">.</span><span class=\"n\">register</span><span class=\"p\">(</span><span class=\"n\">Question</span><span class=\"p\">)</span>\n</code></pre></figure>\n</section>\n<section id=\"explore-the-free-admin-functionality\">\n<h3>Explore the free admin functionality<a class=\"heading-anchor\" href=\"#explore-the-free-admin-functionality\"><span class=\"visually-hidden\">Link to this heading</span><span aria-hidden=\"true\">#</span></a></h3>\n<p>Now that we’ve registered <code class=\"docutils literal notranslate\"><span class=\"pre\">Question</span></code>, Django knows that it should be displayed on\nthe admin index page:</p>\n<img alt=\"Django admin index page, now with polls displayed\" src=\"/en/1.11/_images/admin03t.png\" />\n<p>Click “Questions”. Now you’re at the “change list” page for questions. This page\ndisplays all the questions in the database and lets you choose one to change it.\nThere’s the “What’s up?” question we created earlier:</p>\n<img alt=\"Polls change list page\" src=\"/en/1.11/_images/admin04t.png\" />\n<p>Click the “What’s up?” question to edit it:</p>\n<img alt=\"Editing form for question object\" src=\"/en/1.11/_images/admin05t.png\" />\n<p>Things to note here:</p>\n<ul class=\"simple\">\n<li><p>The form is automatically generated from the <code class=\"docutils literal notranslate\"><span class=\"pre\">Question</span></code> model.</p></li>\n<li><p>The different model field types (<a class=\"reference internal\" href=\"/en/1.11/ref/models/fields/#django.db.models.DateTimeField\" title=\"django.db.models.DateTimeField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">DateTimeField</span></code></a>,\n<a class=\"reference internal\" href=\"/en/1.11/ref/models/fields/#django.db.models.CharField\" title=\"django.db.models.CharField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">CharField</span></code></a>) correspond to the appropriate HTML\ninput widget. Each type of field knows how to display itself in the Django\nadmin.</p></li>\n<li><p>Each <a class=\"reference internal\" href=\"/en/1.11/ref/models/fields/#django.db.models.DateTimeField\" title=\"django.db.models.DateTimeField\"><code class=\"xref py py-class docutils literal notranslate\"><span class=\"pre\">DateTimeField</span></code></a> gets free JavaScript\nshortcuts. Dates get a “Today” shortcut and calendar popup, and times get\na “Now” shortcut and a convenient popup that lists commonly entered times.</p></li>\n</ul>\n<p>The bottom part of the page gives you a couple of options:</p>\n<ul class=\"simple\">\n<li><p>Save – Saves changes and returns to the change-list page for this type of\nobject.</p></li>\n<li><p>Save and continue editing – Saves changes and reloads the admin page for\nthis object.</p></li>\n<li><p>Save and add another – Saves changes and loads a new, blank form for this\ntype of object.</p></li>\n<li><p>Delete – Displays a delete confirmation page.</p></li>\n</ul>\n<p>If the value of “Date published” doesn’t match the time when you created the\nquestion in <a class=\"reference internal\" href=\"/en/1.11/intro/tutorial01/\"><span class=\"doc\">Tutorial 1</span></a>, it probably\nmeans you forgot to set the correct value for the <a class=\"reference internal\" href=\"/en/1.11/ref/settings/#std-setting-TIME_ZONE\"><code class=\"xref std std-setting docutils literal notranslate\"><span class=\"pre\">TIME_ZONE</span></code></a> setting.\nChange it, reload the page and check that the correct value appears.</p>\n<p>Change the “Date published” by clicking the “Today” and “Now” shortcuts. Then\nclick “Save and continue editing.” Then click “History” in the upper right.\nYou’ll see a page listing all changes made to this object via the Django admin,\nwith the timestamp and username of the person who made the change:</p>\n<img alt=\"History page for question object\" src=\"/en/1.11/_images/admin06t.png\" />\n<p>When you’re comfortable with the models API and have familiarized yourself with\nthe admin site, read <a class=\"reference internal\" href=\"/en/1.11/intro/tutorial03/\"><span class=\"doc\">part 3 of this tutorial</span></a> to learn\nabout how to add more views to our polls app.</p>\n</section>\n</section>","rootId":"writing-your-first-django-app-part-2","toc":[{"title":"Database setup","anchor":"database-setup","children":[]},{"title":"Creating models","anchor":"creating-models","children":[]},{"title":"Activating models","anchor":"activating-models","children":[]},{"title":"Playing with the API","anchor":"playing-with-the-api","children":[]},{"title":"Introducing the Django Admin","anchor":"introducing-the-django-admin","children":[{"title":"Creating an admin user","anchor":"creating-an-admin-user","children":[]},{"title":"Start the development server","anchor":"start-the-development-server","children":[]},{"title":"Enter the admin site","anchor":"enter-the-admin-site","children":[]},{"title":"Make the poll app modifiable in the admin","anchor":"make-the-poll-app-modifiable-in-the-admin","children":[]},{"title":"Explore the free admin functionality","anchor":"explore-the-free-admin-functionality","children":[]}]}],"breadcrumbs":[{"docname":"intro/index","title":"Getting started","url":"/en/1.11/intro/"}],"prev":{"docname":"intro/tutorial01","title":"Writing your first Django app, part 1","url":"/en/1.11/intro/tutorial01/"},"next":{"docname":"intro/tutorial03","title":"Writing your first Django app, part 3","url":"/en/1.11/intro/tutorial03/"},"formats":{"html":"/en/1.11/intro/tutorial02/","markdown":"/en/1.11/intro/tutorial02.md","json":"/en/1.11/intro/tutorial02.json"},"source":"https://github.com/django/django/blob/stable/1.11.x/docs/intro/tutorial02.txt","official":"https://docs.djangoproject.com/en/1.11/intro/tutorial02/","inVersions":["dev","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","1.9","1.8"],"inLocales":["en","fr","ja","id","pt-br","ko","es","el","pl"]}