what can I do so that I can set Choices from the Question model in the django's admin page, where Question is a ForeignKey field to the Choices?

models.py:-

from django.db import models
from datetime import timedelta
from django.utils import timezone

# Create your models here.

class Question(models.Model):
    question_text = models.CharField(max_length=500)
    pub_date = models.DateTimeField("date published")

    def __str__(self):
        return self.question_text
    
    def is_recent(self):
        return timezone.now()>= self.pub_date >= timezone.now() - timedelta(days=1)

class Choice(models.Model):
    choice_text = models.CharField(max_length=500)
    votes = models.PositiveIntegerField(default=0)
    question = models.ForeignKey(Question, on_delete=models.CASCADE, related_name="choices")

    def __str__(self):
        return self.choice_text

admin.py:-

from django.contrib import admin
from .models import *

# Register your models here.

admin.site.register(Question)
admin.site.register(Choice)

See the docs at InlineModelAdmin

1 Like