Steps to Define Models 1. Open the polls/models.py file. 2. Add the following code: from django.db import models class Question(models.Model): question_text = models.CharField(max_length=200) # Question text pub_date = models.DateTimeField('date published') # Date and time of publication class Choice(models.Model): question = models.ForeignKey(Question, on_delete=models.CASCADE) # Links to Question choice_text = models.CharField(max_length=200) # Choice text votes = models.IntegerField(default=0) # Vote count ________________________________________ Steps to Create Migrations • Open the terminal and run: python manage.py makemigrations polls • Apply the migrations to update the database: python manage.py migrate ________________________________________ Steps to Add Methods 1. Open polls/models.py and update the models: import datetime from django.db import models from django.utils import timezone class Question(models.Model): question_text = models.CharField(max_length=200) pub_date = models.DateTimeField('date published') def __str__(self): return self.question_text def was_published_recently(self): now = timezone.now() return now - datetime.timedelta(days=1)
Steps to Define Models
1. Open the polls/models.py file.
2. Add the following code:
from django.db import models
class Question(models.Model):
question_text = models.CharField(max_length=200) # Question text
pub_date = models.DateTimeField('date published') # Date and time of publication
class Choice(models.Model):
question = models.ForeignKey(Question, on_delete=models.CASCADE) # Links to Question
choice_text = models.CharField(max_length=200) # Choice text
votes = models.IntegerField(default=0) # Vote count
________________________________________
Steps to Create Migrations
• Open the terminal and run:
python manage.py makemigrations polls
• Apply the migrations to update the database:
python manage.py migrate
________________________________________
Steps to Add Methods
1. Open polls/models.py and update the models:
import datetime
from django.db import models
from django.utils import timezone
class Question(models.Model):
question_text = models.CharField(max_length=200)
pub_date = models.DateTimeField('date published')
def __str__(self):
return self.question_text
def was_published_recently(self):
now = timezone.now()
return now - datetime.timedelta(days=1)
📌 Commands used in this tutorial:
python manage.py makemigrations polls
python manage.py migrate
python manage.py runserver
www.udemy.com/course/mastering-django/?referralCode=43DF73452FECC31E90FC