尝试通过浏览另一个模型

时间:2018-02-23 15:51:32

标签: python django django-forms

我对查询不太满意,所以我需要一些帮助。我有一个Course模型和一个TeacherData模型。每位老师都有自己的课程。但是当教师开帐户时我会使用教师模型。如果TeacherData的teacher_ID列与用户输入的列不匹配,则无法创建帐户。  现在,在我的表单中,我需要为每位教师显示课程,以便可以创建Lecture实例。我正在考虑使用teacher_ID作为连接教师模型和TeacherData模型的桥梁,然后我将能够显示只有特定教师的课程。 这是我尝试过的,但无法弄清楚:



class Teacher(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, primary_key=True)
    name = models.CharField(max_length=30, null=True, blank=True, default=None)
    surname = models.CharField(max_length=50, null=True, blank=True, default=None)
    email = models.EmailField(unique=True, null=True, blank=True, default=None)
    teacher_ID = models.CharField(unique=True, max_length=14,
                                  validators=[RegexValidator(regex='^.{14}$',
                                                             message='The ID needs to be 14 characters long.')],
                                  null=True, blank=True, default=None)
                                  

class Lecture(models.Model):
    LECTURE_CHOICES = (
        ('Courses', 'Courses'),
        ('Seminars', 'Seminars'),
    )
    course = models.ForeignKey('Course', on_delete=models.CASCADE, default='', related_name='lectures',)
    lecture_category = models.CharField(max_length=10, choices=LECTURE_CHOICES, default='Courses',)
    lecture_title = models.CharField(max_length=100, blank=True, null=True)
    content = models.TextField(blank=False, default=None)


class Course(models.Model):
    study_programme = models.ForeignKey('StudyProgramme', on_delete=models.CASCADE, default='')
    name = models.CharField(max_length=50, unique=True)
    ects = models.PositiveSmallIntegerField(validators=[MaxValueValidator(99)])
    description = models.TextField()
    year = models.PositiveSmallIntegerField(validators=[MaxValueValidator(99)])
    semester = models.IntegerField(choices=((1, "1"),
                                            (2, "2"),
                                            ), default=None)
    teacher1 = models.ForeignKey('TeacherData', on_delete=models.CASCADE, default=None,
                                 verbose_name="Course Teacher", related_name='%(class)s_course_teacher')
    teacher2 = models.ForeignKey('TeacherData', on_delete=models.CASCADE, default=None, null=True,
                                 verbose_name="Seminar Teacher", related_name='%(class)s_seminar_teacher')

class TeacherData(models.Model):
    name = models.CharField(max_length=30)
    surname = models.CharField(max_length=50)
    teacher_ID = models.CharField(unique=True, max_length=14)
    notes = models.CharField(max_length=255, default=None, blank=True)
 Run code snippetExpand snippet

class LectureForm(forms.ModelForm):
    lecture_title = forms.CharField(max_length=100, required=True)
    course = forms.ModelChoiceField(initial=Course.objects.first(), queryset=Course.objects.filter(
        Q(teacher1__surname__in=[t.surname for t in TeacherData.objects.filter(
            Q(teacher_ID__iexact=[x.teacher_ID for x in Teacher.objects.all()]))])))

    class Meta:
        model = Lecture
        fields = ('course', 'lecture_category', 'lecture_title', 'content')




预期的行为是教师登录时,他们只能为他们教授的课程添加新的讲座。通过在模板中执行以下操作,我可以获得大致相同的效果

                {% for data in teacher_data %}
                    {% if data.teacher_ID == user.teacher.teacher_ID %}
                        {% for course in courses %}
                            {% if course.teacher1 == data or course.teacher2 == data %}
                                 <li>
                                    <a href="{% url 'courses:courses' slug=course.slug %}">{{ course.name }}</a>
                                </li>
                            {% endif %}
                        {% endfor %}
                    {% endif %}
                {% endfor %}

0 个答案:

没有答案