在我的模型中,我有:
class StudentProfile(models.Model):
# Relational fields
#more fields
sports_assigned = models.ManyToManyField('Sport', through="StudentSportAssociation")
我的模型形式如下:
class UpdateStudentForm(ModelForm):
def __init__(self, *args, **kwargs):
super(UpdateStudentForm, self).__init__(*args, **kwargs)
class Meta:
model = StudentProfile
sports_assigned = forms.ModelMultipleChoiceField(queryset=SportWithLevel.objects.all(),
widget=FilteredSelectMultiple("Select", is_stacked=False), required=True)
通过表是:
class StudentSportAssociation(AbstractBaseModel):
"""
Association of student to a sport and his current level in the sport
"""
sport = models.ForeignKey('Sport')
level = models.ForeignKey('Level')
student = models.ForeignKey('StudentProfile', related_name="sports_with_levels")
# save and all follows
现在我需要访问
学生体育协会
访问表单时“通过”表。现在它从 Sport 模型中获取值。有什么办法可以打破这种正常的方式并从直通表中获取详细信息吗?