[Answer]-Django model for time-table like objects

1👍

Here is my implementation

models.py

class TimeTable(models.Model):
    DAYS_OF_THE_WEEK = (
        ('1', 'Понедельник'),
        ('2', 'Вторник'),
        ('3', 'Среда'),
        ('4', 'Четверг'),
        ('5', 'Пятница'),
        ('6', 'Суббота'),
        ('7', 'Воскресеньe')
    )
    course = models.ForeignKey(Course, on_delete=models.CASCADE)
    day = models.CharField(max_length=1, choices=DAYS_OF_THE_WEEK)

    class Meta:
        verbose_name = 'Расписание'
        verbose_name_plural = 'Расписания'


class TimeTableItem(models.Model):
    time_table = models.ForeignKey(TimeTable, on_delete=models.CASCADE)
    subject = models.ForeignKey(Subject, on_delete=models.CASCADE)
    teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE)
    start_time = models.TimeField()
    end_time = models.TimeField()

    def __str__(self):
        return 'Предмет:{}, Учитель:{}, Время:{}-{}'.format(self.subject, self.teacher, self.start_time, self.end_time)

    class Meta:
        ordering = ('start_time',)

admin.py

class TimeTableItemInline(admin.TabularInline):
    model = models.TimeTableItem
    raw_id_fields = ['subject']
    extra = 1


@admin.register(models.TimeTable)
class TimeTableAdmin(admin.ModelAdmin):
    list_display = ['course', 'day']
    inlines = [TimeTableItemInline]

Leave a comment