如何像Django Admin中那样实现FilteredSelectMultiple?

时间:2018-07-21 06:01:41

标签: python django views backend formview

我正在尝试创建一个FilteredSelectMultiple,类似于用户端Django Admin中的组/权限分配。我设法使界面小部件已经显示(每个角色有5个)。但是我不确定后端逻辑。我目前正在为此使用FormView。我想知道:

  • 如何在用户下次返回时在选定项中显示项目
  • 当我单击“保存”按钮时,如何让Django知道哪些项是新添加到列表中以及删除了哪些项。

我的想法是有两个列表:一个包含所有已保存到数据库中的记录(下次用户输入时应显示在选定内容中的项目),另一个是后一个最新列表。用户保存(它包含较新的元素和/或可能删除了一些旧元素)。但是我不确定这是正确的还是有更好的方法。

这些是我的代码(预先致歉,views.py尚不完整,因此目前使用打印):

forms.py:

class DesignationForm(forms.ModelForm):
class Media:
    css = {
        'all': ('admin/css/responsive.css', 'admin/css/base.css', 'admin/css/widgets.css',)
    }
    js = ('/admin/jsi18n',)

class Meta:
    model = Designations
    fields = ['module_coordinator', 'instructor', 'moderator', 'verifier', 'invigilator']

module_coordinator = forms.ModelMultipleChoiceField(queryset=Staffs.objects.all(),
                                                        widget=FilteredSelectMultiple("Module Coordinator",
                                                                                      is_stacked=False),
                                                    required=False,
                                                    label="")
instructor = forms.ModelMultipleChoiceField(queryset=Staffs.objects.all(),
                                            widget=FilteredSelectMultiple("Instructor",
                                                                          is_stacked=False),
                                            required=False,
                                            label="")
moderator = forms.ModelMultipleChoiceField(queryset=Staffs.objects.all(),
                                           widget=FilteredSelectMultiple("Moderator",
                                                                         is_stacked=False),
                                           required=False,
                                           label="")
verifier = forms.ModelMultipleChoiceField(queryset=Staffs.objects.all(),
                                          widget=FilteredSelectMultiple("Verifier",
                                                                        is_stacked=False),
                                          required=False,
                                          label="")
invigilator = forms.ModelMultipleChoiceField(queryset=Staffs.objects.all(),
                                             widget=FilteredSelectMultiple("invigilator",
                                                                           is_stacked=False),
                                             required=False,
                                             label="")

views.py:

class DesignationsView(FormView):
form_class = DesignationForm
template_name = 'curricula/designations.html'

def form_valid(self, form):
    # We need a before list for each role (Get this from the database since it already exists)
    # Module Coordinator
    already_chosen_mc = Designations.objects.filter(role__role_name='Module Coordinator',
                                                    curriculum__course_period__course__course_abbreviation=self.kwargs.get(
                                                        'course_abbr'),
                                                    curriculum__module_period__module__module_abbreviation=self.kwargs.get(
                                                        'module_abbr'),
                                                    curriculum__course_period__period__academic_year=self.kwargs.get(
                                                        'year'),
                                                    curriculum__course_period__period__semester=self.kwargs.get(
                                                        'semester'),
                                                    curriculum__stage=self.kwargs.get('stage')).values_list(
        'staff__staff_id', flat=True)
    print(already_chosen_mc)
    # Instructor
    already_chosen_instructor = Designations.objects.filter(role__role_name='Instructor',
                                                            curriculum__course_period__course__course_abbreviation=self.kwargs.get(
                                                                'course_abbr'),
                                                            curriculum__module_period__module__module_abbreviation=self.kwargs.get(
                                                                'module_abbr'),
                                                            curriculum__course_period__period__academic_year=self.kwargs.get(
                                                                'year'),
                                                            curriculum__course_period__period__semester=self.kwargs.get(
                                                                'semester'),
                                                            curriculum__stage=self.kwargs.get('stage')).values_list(
        'staff__staff_id', flat=True)
    print(already_chosen_instructor)
    # Moderator
    already_chosen_moderator = Designations.objects.filter(role__role_name='Moderator',
                                                           curriculum__course_period__course__course_abbreviation=self.kwargs.get(
                                                               'course_abbr'),
                                                           curriculum__module_period__module__module_abbreviation=self.kwargs.get(
                                                               'module_abbr'),
                                                           curriculum__course_period__period__academic_year=self.kwargs.get(
                                                               'year'),
                                                           curriculum__course_period__period__semester=self.kwargs.get(
                                                               'semester'),
                                                           curriculum__stage=self.kwargs.get('stage')).values_list(
        'staff__staff_id', flat=True)
    print(already_chosen_moderator)
    # Verifier
    already_chosen_verifier = Designations.objects.filter(role__role_name='Verifier',
                                                          curriculum__course_period__course__course_abbreviation=self.kwargs.get(
                                                              'course_abbr'),
                                                          curriculum__module_period__module__module_abbreviation=self.kwargs.get(
                                                              'module_abbr'),
                                                          curriculum__course_period__period__academic_year=self.kwargs.get(
                                                              'year'),
                                                          curriculum__course_period__period__semester=self.kwargs.get(
                                                              'semester'),
                                                          curriculum__stage=self.kwargs.get('stage')).values_list(
        'staff__staff_id', flat=True)
    print(already_chosen_verifier)
    # Invigilator
    already_chosen_invigilator = Designations.objects.filter(role__role_name='Invigilator',
                                                             curriculum__course_period__course__course_abbreviation=self.kwargs.get(
                                                                 'course_abbr'),
                                                             curriculum__module_period__module__module_abbreviation=self.kwargs.get(
                                                                 'module_abbr'),
                                                             curriculum__course_period__period__academic_year=self.kwargs.get(
                                                                 'year'),
                                                             curriculum__course_period__period__semester=self.kwargs.get(
                                                                 'semester'),
                                                             curriculum__stage=self.kwargs.get(
                                                                 'stage')).values_list(
        'staff__staff_id', flat=True)
    print(already_chosen_invigilator)

    # Now, we get the current lists, this lists contains both exist in, newly added
    # and may have missing staffs, if they have been removed
    # Module Coordinator
    chosen_mc = self.request.POST.getlist('module_coordinator')
    print(chosen_mc)
    # Instructor
    chosen_instructor = self.request.POST.getlist('instructor')
    print(chosen_instructor)
    # Moderator
    chosen_moderator = self.request.POST.getlist('moderator')
    print(chosen_moderator)
    # Verifier
    chosen_verifier = self.request.POST.getlist('verifier')
    print(chosen_verifier)
    # Invigilator
    chosen_invigilator = self.request.POST.getlist('invigilator')
    print(chosen_invigilator)

    # At this point, we now have two lists for each designations. One before the save, and
    # one after. Compare the two and find the differences
    # between the two lists. Either staff(s) are removed or added.
    # If the staffs in before and after matches, ignore
    # Firstly, check if the before list is empty (no records found in DB)
    # If the old list is empty, we know the after list are new elements to be added
    # into the db
    if not already_chosen_mc:
        print("I am empty! Feed me!")
    # Else if the old element has elements, check if the new lists elements have some that are newly included and/or removed (Unsure of how to do this)

非常感谢!

0 个答案:

没有答案