Creating Models for a Poll App with Django - Step-by-Step Tutorial

Поделиться
HTML-код
  • Опубликовано: 31 янв 2025

Комментарии • 3

  • @AzadRasul1977
    @AzadRasul1977  11 дней назад +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)

  • @AzadRasul1977
    @AzadRasul1977  11 дней назад

    📌 Commands used in this tutorial:
    python manage.py makemigrations polls
    python manage.py migrate
    python manage.py runserver

  • @AzadRasul1977
    @AzadRasul1977  5 дней назад

    www.udemy.com/course/mastering-django/?referralCode=43DF73452FECC31E90FC