Django modelForm

时间:2019-06-21 01:13:07

标签: django django-forms

我有个人资料,工作,学历和当前的职业模式。

模型之间的关系,

  

个人资料-------很多很多------工作

     

个人资料-------多对多------教育

     

个人资料------- OneToOne ------当前居住情况

我想要一个当前职位的下拉列表,它将具有所有工作和教育价值。

到目前为止,我可以显示工作经历的职位名称,

enter image description here

我想要的是这样的下拉列表,以显示来自教育表和组织的所有价值,

enter image description here

我应该如何处理?

个人资料

class Profile(models.Model):
    user               = models.OneToOneField(CustomUser, on_delete=models.CASCADE, null=True, blank=True)
    full_name          = models.CharField(max_length=30, null=True, blank=True)
    experience         = models.ManyToManyField(Work_Experience, blank=True)
    education          = models.ManyToManyField(Education, blank=True)
    current_occupation = models.OneToOneField(Current_occupation, on_delete=models.CASCADE, null=True, blank=True)

教育

class Education(models.Model):
    degree          = models.CharField(max_length=100, null=True, blank=True)
    school          = models.CharField(max_length=100, null=True, blank=True)

经验

class Work_Experience(models.Model):
    job_title       = models.CharField(max_length=100, null=True, blank=True)
    company         = models.CharField(max_length=100, null=True, blank=True)

当前职业

class Current_occupation(models.Model):
    title = models.CharField(max_length=100, null=True, blank=True)
    organization = models.CharField(max_length=100, null=True, blank=True)

表格

class ProfileSettingsForm(forms.ModelForm):

    class Meta:
        model = Profile

        fields = ['image','full_name','biography','profile_email','linked_in','facebook',
                  'twitter','phone','education', 'is_active', 'current_occupation']

    def __init__(self, request, *args, **kwargs):
        super(ProfileSettingsForm, self).__init__(*args, **kwargs)
        self.request = request
        self.fields['current_occupation'].queryset = Profile.objects.filter(user=self.request.user.profile.id).values_list('experience__job_title', flat=True)

查看

class ProfileSettingsView(UpdateView):
    model = Profile
    form_class = ProfileSettingsForm
    pk_url_kwarg = 'pk'
    context_object_name = 'object'
    template_name = 'profile_settings.html'

    def get_success_url(self):
          return reverse_lazy('users:profile_settings', args = (self.object.id,))

    def get_object(self):
        pk = self.kwargs.get('pk')
        return get_object_or_404(Profile, id=pk)

    def get_form_kwargs(self):
        kwargs = super(ProfileSettingsView, self).get_form_kwargs()
        kwargs['request'] = self.request
        return kwargs

0 个答案:

没有答案