python manage.py startapp polls
# In project1/settings.py file
INSTALLED_APPS = [
'polls.apps.PollsConfig',
# Below are the pre-installed apps which were already present
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
foo
, you’d have added foo.apps.FooConfig
in INSTALLED_APPS list.# In project1/polls/views.py file
from django.shortcuts import render, HttpResponse
def hello_world(request):
print("This is my first view! Hello World!")
return HttpResponse("Hello World!")
http://127.0.0.1:8000/
).# In project1/urls.py file
from django.urls import path, include
path("/", include("polls.urls")),
# In project1/urls.py file
urlpatterns = [
path("", include("polls.urls")),
path("admin/", admin.site.urls),
]
# In project1/polls/urls.py file
from django.urls import path
from polls import views
urlpatterns = [
path("", views.hello_world),
]
python manage.py runserver
http://127.0.0.1:8000/
on your browser and you must see Hello World! there.This is my first view! Hello World!
printed there. WHY?
Instances/Objects of Model (Class) represent Rows/Tuple in Table.
Object Oriented Programming | Database |
---|---|
Class | Table |
Class Variables | Columns |
Instances/Objects of Class | Rows/Tuple in table |
# In project1/polls/models.py
class Question(models.Model): # Model class from models module is inherited in class Question.
question_text = models.CharField(max_length=1000, verbose_name="Text")
publication_date = models.DateField(verbose_name="Publication Date")
def __str__(self): # A method is declared so that in admin panel Text of Question is displayed.
return self.question_text
Note that we have not added question_id field. Why? This is because for each model, Django creates a Primary Key by default. It’s name is id or pk.
For Choices Table, write the following code
# In project1/polls/models.py
class Choice(models.Model):
choice_text = models.CharField(max_length=200, verbose_name="Choice Text")
number_of_votes = models.IntegerField(verbose_name="Number of Votes")
question = models.ForeignKey(Question, on_delete=models.CASCADE)
def __str__(self):
return self.choice_text
models.CASCADE - Cascade means that if a table is deleted, then all it’s related from in other tables is also deleted. Example - In this case when a Question is deleted from database, Choices corresponding to that question will also be deleted.
models.DO_NOTHING - In this case on deleting a Question, Choices related to that question won’t be deleted.
models.RESTRICT - This will not allow any Question to be deleted as long as there is a Choice present for that question. If we want to delete the Question, firstly we will have to delete all the Choices related to that question.
python manage.py migrate
python manage.py makemigrations polls
OR
python manage.py makemigrations
python manage.py migrate
python manage.py sqlmigrate polls 0001_initial
python manage.py runserver
http://127.0.0.1:8000/admin
. A page will open asking for username and password.Ctrl + C
to stop the server and run the following command in PyCharm’s terminal.python manage.py createsuperuser
Username: admin
Email: admin@admin.com
Password:
Password (again):
http://127.0.0.1:8000/admin
. Use the credentials of superuser to enter into Admin Panel.# In project1/polls/admin.py file
from .models import Question, Choice
admin.site.register(Question)
admin.site.register(Choice)
python manage.py makemigrations
command. Migrated files are created. These files contains queries written by Django in selected Database Engine in a .py file.python manage.py migrate
command. Actual Tables/Database is created.Step 4 - Register the models in admin.py and visit Django Admin Panel to interact with the database.