我有一个父型号Profile
和另外两个型号Courses
和Course_student
。
class Profile(models.Model):
...
first_name = models.CharField(verbose_name=_('First Name'), max_length=255, null=True, blank=True, )
middle_name = models.CharField(verbose_name=_('Middle Name'), max_length=255, null=True, blank=True)
last_name = models.CharField(verbose_name=_('Last Name'), max_length=255, null=True, blank=True)
...
class Courses(models.Model):
YESNO = (
('Yes', 'Yes'),
('No', 'No'),
)
profile = models.ManyToManyField('Profile', related_name='course_profile', verbose_name=_('Profile'))
course_taken = models.CharField(verbose_name=_('Course'), max_length=255)
name_of_school = models.CharField(verbose_name=_('Name of School'), max_length=255)
school_location = models.CharField(verbose_name=_('School Location'), max_length=255)
period_of_attendance_from = models.DateField(verbose_name=_('Period of Attendance (From)'), max_length=255)
period_of_attendance_to = models.DateField(verbose_name=_('Period of Attendance (To)'), max_length=255)
nr_students = models.IntegerField(verbose_name=_('Total Number of Students'), null=True, blank=True)
created_on = models.DateTimeField(auto_now_add=True)
modified_on = models.DateTimeField(auto_now=True)
class Meta:
verbose_name = _('Course')
verbose_name_plural = _('Course')
def __str__(self):
return '%s' % (self.course_taken)
class Course_student(models.Model):
YESNO = (
('Yes', 'Yes'),
('No', 'No'),
)
course = models.ForeignKey('Courses', related_name='course_student_details', on_delete=models.DO_NOTHING, verbose_name=_('Course'), blank=True, null=True)
profile = models.ForeignKey('Profile', related_name='course_student_profile', on_delete=models.DO_NOTHING, verbose_name=_('Profile'), blank=True, null=True)
standing = models.IntegerField(verbose_name=_('Standing'), null=True, blank=True)
grade = models.CharField(verbose_name=_('Grade'), max_length=255, null=True, blank=True)
completed = models.CharField(verbose_name=_('Completed'), choices=YESNO, max_length=255)
class Meta:
verbose_name = _('Student Details')
verbose_name_plural = _('Student Details')
def __str__(self):
return '%s' % (self.course)
admin.py
class CourseUploaderInline(admin.StackedInline):
model = CourseUploader
extra = 1
class CourseStudentInline(admin.StackedInline):
model = Course_student
extra = 1
@admin.register(Courses)
class CoursesAdmin(admin.ModelAdmin):
inlines = [Course_studentInline, CorseUploaderInline,]
Course_student
具有Foreignkey
到Courses
和Profile
- 正如您所看到的,
Course_student
具有用于profile
、standing
、grade
和completed
的字段,这将保存学生的详细信息,为特定的课程和特定的配置文件/学生。而Courses
有M2M
到Profile
,以便我可以标记学生什么课程,她/他有。Profile
将保存学员的基本信息
我尝试做的是,通过使用Profile
进行过滤来合并Courses
和Course_student
,例如:
x1米20英寸1x
def profile(request, pk):
profile = Profile.objects.filter(profile=pk)
course = Course.objects.filter(profile=pk) # I tried here the select_related()
P.S.我是Django和Python的新手,我希望你能对我宽容:)TIA也欢迎你对我正在努力实现的目标提出建议。
1条答案
按热度按时间qkf9rpyu1#
在使用
select_related()
对我的查询集进行故障排除后,我发现问题出在显示“合并course
和course_student
列表”的模板语法上我在模板中使用了语法
{{ course.name_of_school }}
而不是{{ course.course.name_of_school }}
。使用select_related()
是遵循反向关系的正确方法。我只是错过了模板上的语法。course = Course_student.objects.select_related('course').filter(profile=pk)
我终于可以睡了。