{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Chapter 4: Models\n",
    "\n",
    "See also [Writing your first Django app, part 2](https://docs.djangoproject.com/en/5.0/intro/tutorial02/).\n",
    "\n",
    "## Database setup"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Database settings in `settings.py`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "DATABASES = {\n",
    "    'default': {\n",
    "        'ENGINE': 'django.db.backends.sqlite3',\n",
    "        'NAME': BASE_DIR / 'db.sqlite3',\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Run initial migrations:"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "vscode": {
     "languageId": "raw"
    }
   },
   "source": [
    "$ python manage.py migrate"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating a simple model\n",
    "\n",
    "Create model `Post` in `models.py`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from django.db import models\n",
    "\n",
    "# Create your models here.\n",
    "\n",
    "class Post(models.Model):\n",
    "    title = models.CharField(max_length=100)\n",
    "    text = models.TextField(max_length=1000)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Register model in `admin.py`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from django.contrib import admin\n",
    "\n",
    "from .models import Post\n",
    "\n",
    "# Register your models here.\n",
    "\n",
    "admin.site.register(Post)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create and execute migrations."
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "vscode": {
     "languageId": "raw"
    }
   },
   "source": [
    "$ python manage.py makemigrations chp04_models\n",
    "$ python manage.py migrate"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Explore admin interface at http://localhost:8000/admin"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Creating relationships\n",
    "\n",
    "Create model `Category` in `models.py` **above** the `Post` model:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from django.db import models\n",
    "\n",
    "# Create your models here.\n",
    "\n",
    "class Category(models.Model):\n",
    "    title = models.CharField(max_length=100)\n",
    "\n",
    "# ..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Register model in `admin.py`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from django.contrib import admin\n",
    "\n",
    "from .models import Category, Post\n",
    "\n",
    "# Register your models here.\n",
    "\n",
    "admin.site.register(Category) # NEW\n",
    "admin.site.register(Post)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Add relation `category` to model `Post`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# ...\n",
    "\n",
    "class Post(models.Model):\n",
    "    title = models.CharField(max_length=100)\n",
    "    text = models.TextField(max_length=1000)\n",
    "    category = models.ForeignKey(Category, on_delete=models.SET_NULL)\n",
    "\n",
    "# ..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Create and execute migrations:"
   ]
  },
  {
   "cell_type": "raw",
   "metadata": {
    "vscode": {
     "languageId": "raw"
    }
   },
   "source": [
    "$ python manage.py makemigrations chp04_models\n",
    "$ python manage.py migrate"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
