Несколько списков в modelChoiceField в django modelForm - PullRequest
0 голосов
/ 21 июня 2019

У меня есть Профиль, работа, образование и текущие профессии.

Отношения между моделями,

Профиль ------- ManytoMany ------Работа

Профиль ------- ManyToMany ------ Образование

Профиль ------- OneToOne ------ Current_occupation

Я хочу раскрывающийся список для текущей профессии, который будет иметь все значения работы и образования.

Пока я могу показать название должности с опытом работы,

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)

Current_occupation

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
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...