R
R
R4ndolphC4rter2021-01-01 17:03:36
Django
R4ndolphC4rter, 2021-01-01 17:03:36

How to correctly design the following database to display correctly in the Django admin panel?

Foreword:
As a student, I am working on a small project, unfortunately, almost alone. The bottom line is that I need to write a web application that would give the N-th number of docx files at the output (various test options + answers to each of the options). The input, respectively, is information about the school subject for which options will be generated, and N is the number of these same options.
Options are created in the following way: the database stores a lot of questions (on different subjects) and answers to them, a certain number of tasks are selected from this set, and then the conditions of these tasks and answer options (if the task implies answer options) are written to one file, and the correct answers to these tasks are recorded in another file - the answer file.

Condition:
The body of the task itself, as well as the answer options for this task (if these options are implied), can consist of plain text, latex formulas and pictures (one of these must be present, the rest may or may not). In each subject, the test consists of M number of tasks. Tasks with index i can take i place in the test. Those. for example, in the history test, one (randomly selected) of a dozen questions with an index i equal to exactly 5 will take the fifth place. Questions related to Peter the Great will be (all this is conditional) stored on this index.

My implementation attempts:
I decided that the database would start with a table with items.
61d0557979bee104785844.jpeg
With fields: subject name (nominative case), dative case (for displaying information on an html page), the number of tasks in one option task_number for this subject. Those. index i will take values ​​from 0 to task_number not inclusive.

Then there will be a tasks table that stores the task condition.
61d056dac2d03895911126.jpeg
Here num is the task index, text is the text field of the task, latex is a text field for storing the formula in the appropriate format, image is a link to the image for the task, subject_fk_id is a foreign key that refers to the id of the subject to which this task belongs.

And the third table: options.
61d057bae2f85428443868.jpeg
Fields text, latex, image are similar to those described above, is_answer - boolean values ​​(1 - correct answer, 0 - no), task_fk_id - foreign key to the task to which the answers belong.
It seemed to me (with my little experience) that this implementation is not bad, but there were a number of problems (some of which are related to this question) with the django admin panel. After all, users of this system need to be given the opportunity to add / delete / edit tasks.

Code:
The following code snippets are not used in the main program. I wrote them to figure out how to create models and how to interact with them through the admin panel.

python code

models.py
from django.db import models


# Create your models here.


class Subjects(models.Model):
    case_nominative = models.CharField(max_length=32, verbose_name='Название')
    case_dative = models.CharField(max_length=32, verbose_name='Дательный падеж')
    tasks_number = models.SmallIntegerField(default=0, verbose_name='Количество заданий в варианте')

    def __str__(self):
        return self.case_nominative

    class Meta:
        verbose_name = 'Предмет'
        verbose_name_plural = 'Предметы'
        ordering = ['id']


class Tasks(models.Model):
    subject_fk = models.ForeignKey(Subjects, on_delete=models.CASCADE, verbose_name='Ключ предмета')
    num = models.SmallIntegerField(verbose_name='Номер задания в тесте')
    text = models.TextField(blank=True, verbose_name='Текст задания')
    latex = models.TextField(blank=True, verbose_name='Формула (LaTeX)')
    image = models.ImageField(upload_to='thumbnails_task/', blank=True,
                              verbose_name='Изображение')

    def __str__(self):
        return str(self.subject_fk) + ' [№' + str(self.num) + '] ' + self.text

    class Meta:
        verbose_name = 'Задание'
        verbose_name_plural = 'Задания'


class Options(models.Model):
    task_fk = models.ForeignKey(Tasks, on_delete=models.CASCADE, verbose_name='Ключ задания')
    text = models.TextField(blank=True, verbose_name='Текст ответа')
    latex = models.TextField(blank=True, verbose_name='Формула (LaTeX)')
    image = models.ImageField(upload_to='thumbnails_option/', blank=True,
                              verbose_name='Изображение')
    is_answer = models.BooleanField(default=False, verbose_name='Верный ответ?')

    def __str__(self):
        return str(self.task_fk) + ' | ' + self.text

    class Meta:
        verbose_name = 'Вариант ответа'
        verbose_name_plural = 'Варианты ответов'


admin.py
Пока без фильтров.
from django.contrib import admin

from .models import *


# Register your models here.


class SubjectsAdmin(admin.ModelAdmin):
    list_display = ('case_nominative', 'tasks_number')
    list_display_links = ('case_nominative',)


class TasksAdmin(admin.ModelAdmin):
    list_display = ('id', 'subject_fk', 'num', 'text', 'latex', 'image')
    list_display_links = ('id', 'text', 'latex')
    # search_fields = ('id', 'subject_fk', 'num', 'text', 'latex')
    search_fields = ('id', 'num', 'text', 'latex')
    ordering = ('subject_fk', 'num')


class OptionsAdmin(admin.ModelAdmin):
    list_display = ('id', 'task_fk', 'text', 'latex', 'image', 'is_answer')
    list_display_links = ('id', 'text')
    search_fields = ('task_fk__id', 'text')
    ordering = ('task_fk', 'id')


admin.site.register(Subjects, SubjectsAdmin)
admin.site.register(Tasks, TasksAdmin)
admin.site.register(Options, OptionsAdmin)



Problem:
The page for adding new tasks (meaning the conditions) suits me, only here is the field
num = models.SmallIntegerField(verbose_name='Номер задания в тесте')
can't limit. Now you can enter any number there, but I would like from 1 to task_number of the corresponding subject from the subjects table (well, or from 0 to task_number-1).
The page for adding answer options (options) turns out to be bad.
61d05a6fa3c17547765044.jpeg
Obviously, this drop-down list is very inconvenient to work with (especially when there are a lot of tasks in the database).
61d05ad8b12e7354331055.jpeg
Hence the questions.

Questions:
How to limit the num field?
How do I fix the above issue with the Add Choices page? Maybe redesign the database or somehow work with the display? Is it possible to somehow embed filters in this dropdown?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dr. Bacon, 2021-01-01
@bacon

continue reading the admin doc and learn about raw_id_fields or autocomplete_fields

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question