Y
Y
Yura2021-07-11 19:54:13
Django
Yura, 2021-07-11 19:54:13

How to create objects in the admin panel?

Hello,

from django.db import models


class Test(models.Model):
    title = models.CharField(max_length=150, verbose_name="Test title")
    topic = models.ForeignKey("Topic", on_delete=models.PROTECT, null=True, verbose_name="topic")

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = "Test"
        verbose_name_plural = "Tests"


class Topic(models.Model):
    title = models.CharField(max_length=150, verbose_name="Topic title")

    def __str__(self):
        return self.title

    class Meta:
        verbose_name = "Topic"
        verbose_name_plural = "Topics"


class Question(models.Model):
    text = models.TextField(verbose_name="Test name")
    photo = models.ImageField(upload_to='photos/%Y/%m/%d/', verbose_name="photo", blank=True)
    test = models.ForeignKey(Test, on_delete=models.PROTECT, null=True, verbose_name="test")

    class Meta:
        verbose_name = "Question"
        verbose_name_plural = "Questions"


class Answer(models.Model):
    text = models.TextField(verbose_name="Answer text")
    is_correct = models.BooleanField(verbose_name="Is correct answer")
    question = models.ForeignKey(Question, on_delete=models.PROTECT, null=True, verbose_name="test")

    class Meta:
        verbose_name = "Answer"
        verbose_name_plural = "Answers"

there are such models, there should be a site for passing tests, but it’s not very convenient to create tests in the admin panel, it turns out you need to first create answers (Answer), then create a question (Question), select answers in it. and therefore, also in Test, select questions, is it possible to do all this when creating the test itself? Or have I created the models incorrectly?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Gornostaev, 2021-07-11
@N0tdefined

https://docs.djangoproject.com/en/3.2/ref/contrib/...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question