我有两个模型:
class Profile(models.Model):
profileName = models.CharField(max_length=50)
class RelatedProfile(models.Model):
relatedProf = models.ManyToManyField(UseCaseProfile, through='UseCaseProfileRelatedUseCase')
relationType= models.CharField(max_length=3, choices=RELATION_CHOICE)
通过遵循ManyToMany关系的Django文档,我在下面创建了此表:
class ProfileRelated(models.Model):
prof = models.ForeignKey(Profile, on_delete=models.CASCADE)
related = models.ForeignKey(RelatedProfile, on_delete=models.CASCADE)
我的目标是拥有一个inlineformset,其中父模型是Profile,子模型是RelatedProfile。因此,我做了以下事情:
forms.py
class ProfileForm(ModelForm):
class Meta:
model = Profile
fields='__all__'
class RelatedUCForm(ModelForm):
class Meta:
model = RelatedUseCase
fields = '__all__'
RelatedUCFormSet = inlineformset_factory(Profile, RelatedProfile.relatedProf.through)
views.py(我省去了不必要的代码)
class ProfileUpdateView(UpdateView):
template_name = "some.html"
model = Profile
form_class = ProfileForm
def get_context_data(self, **kwargs):
context = super(ProfileUpdateView, self).get_context_data(**kwargs)
if self.request.POST:
context['relatedProfile_form'] = RelatedUCFormSet(self.request.POST, self.request.FILES, instance=self.object, prefix='related')
else:
context['relatedProfile_form'] = RelatedUCFormSet(instance=self.object, prefix='related')
return context
基于模型,表单和视图,我要显示如下网页:
<pre>
Profile
profileName: Textfield
Related
Profile: Dropdown with related Profile
:relationType: choicefield
</pre>
但是,当我显示网页时,我没有得到下拉菜单来选择要与父级个人资料相关的个人资料。总体而言,我的问题是要使“相关”下的下拉列表显示我可以选择的个人资料列表。