当前位置: 首页>>代码示例>>Python>>正文


Python forms.ModelMultipleChoiceField方法代码示例

本文整理汇总了Python中django.forms.ModelMultipleChoiceField方法的典型用法代码示例。如果您正苦于以下问题:Python forms.ModelMultipleChoiceField方法的具体用法?Python forms.ModelMultipleChoiceField怎么用?Python forms.ModelMultipleChoiceField使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.forms的用法示例。


在下文中一共展示了forms.ModelMultipleChoiceField方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: __init__

# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ModelMultipleChoiceField [as 别名]
def __init__(self, user=None, *args, **kwargs):
        
        super(ComparisonForm, self).__init__(*args, **kwargs)

        if user.is_staff:
            
            self.fields['individual_one'] = forms.ModelMultipleChoiceField(queryset=Individual.objects.all().order_by('id'), required=False)
            self.fields['individual_two'] = forms.ModelMultipleChoiceField(queryset=Individual.objects.all().order_by('id'), required=False)

        elif not user.is_authenticated:
            
            self.fields['individual_one'] = forms.ModelMultipleChoiceField(queryset=Individual.objects.filter(user=None).order_by('id'), required=False)
            self.fields['individual_two'] = forms.ModelMultipleChoiceField(queryset=Individual.objects.filter(user=None).order_by('id'), required=False)
        else:
            print('user', user)
            self.fields['individual_one'] = forms.ModelMultipleChoiceField(queryset=Individual.objects.filter(user=user).order_by('id'), required=False)
            self.fields['individual_two'] = forms.ModelMultipleChoiceField(queryset=Individual.objects.filter(user=user).order_by('id'), required=False) 
开发者ID:raonyguimaraes,项目名称:mendelmd,代码行数:19,代码来源:forms.py

示例2: __init__

# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ModelMultipleChoiceField [as 别名]
def __init__(self, user=None, *args, **kwargs):
        
        super(FamilyAnalysisForm, self).__init__(*args, **kwargs)

        if not user.is_authenticated:
            # print('user None', user)
            self.fields['mother'] = forms.ModelMultipleChoiceField(queryset=Individual.objects.filter(user=None).order_by('id'), required=False)
            self.fields['father'] = forms.ModelMultipleChoiceField(queryset=Individual.objects.filter(user=None).order_by('id'), required=False)
            self.fields['children'] = forms.ModelMultipleChoiceField(queryset=Individual.objects.filter(user=None).order_by('id'), required=False)
            self.fields['exclude_individuals'] = forms.ModelMultipleChoiceField(queryset=Individual.objects.filter(user=None).order_by('id'), required=False)
        else:
            # print('user', user)
            self.fields['mother'] = forms.ModelMultipleChoiceField(queryset=Individual.objects.filter(user=user).order_by('id'), required=False)
            self.fields['father'] = forms.ModelMultipleChoiceField(queryset=Individual.objects.filter(user=user).order_by('id'), required=False)
            self.fields['children'] = forms.ModelMultipleChoiceField(queryset=Individual.objects.filter(user=user).order_by('id'), required=False)
            self.fields['exclude_individuals'] = forms.ModelMultipleChoiceField(queryset=Individual.objects.filter(user=user).order_by('id'), required=False)


# INDIVIDUALS = [(x.id, x.name) for x in Individual.objects.all().order_by('id')] 
开发者ID:raonyguimaraes,项目名称:mendelmd,代码行数:21,代码来源:forms.py

示例3: test_model_multiple_choice_show_hidden_initial

# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ModelMultipleChoiceField [as 别名]
def test_model_multiple_choice_show_hidden_initial(self):
        """
        Test support of show_hidden_initial by ModelMultipleChoiceField.
        """
        class WriterForm(forms.Form):
            persons = forms.ModelMultipleChoiceField(show_hidden_initial=True, queryset=Writer.objects.all())

        person1 = Writer.objects.create(name="Person 1")
        person2 = Writer.objects.create(name="Person 2")

        form = WriterForm(initial={'persons': [person1, person2]},
                          data={'initial-persons': [str(person1.pk), str(person2.pk)],
                                'persons': [str(person1.pk), str(person2.pk)]})
        self.assertTrue(form.is_valid())
        self.assertFalse(form.has_changed())

        form = WriterForm(initial={'persons': [person1, person2]},
                          data={'initial-persons': [str(person1.pk), str(person2.pk)],
                                'persons': [str(person2.pk)]})
        self.assertTrue(form.is_valid())
        self.assertTrue(form.has_changed()) 
开发者ID:nesdis,项目名称:djongo,代码行数:23,代码来源:tests.py

示例4: test_to_field_name_with_initial_data

# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ModelMultipleChoiceField [as 别名]
def test_to_field_name_with_initial_data(self):
        class ArticleCategoriesForm(forms.ModelForm):
            categories = forms.ModelMultipleChoiceField(Category.objects.all(), to_field_name='slug')

            class Meta:
                model = Article
                fields = ['categories']

        article = Article.objects.create(
            headline='Test article',
            slug='test-article',
            pub_date=datetime.date(1988, 1, 4),
            writer=Writer.objects.create(name='Test writer'),
            article='Hello.',
        )
        article.categories.add(self.c2, self.c3)
        form = ArticleCategoriesForm(instance=article)
        self.assertCountEqual(form['categories'].value(), [self.c2.slug, self.c3.slug]) 
开发者ID:nesdis,项目名称:djongo,代码行数:20,代码来源:tests.py

示例5: test_disabled_multiplemodelchoicefield

# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ModelMultipleChoiceField [as 别名]
def test_disabled_multiplemodelchoicefield(self):
        class ArticleForm(forms.ModelForm):
            categories = forms.ModelMultipleChoiceField(Category.objects.all(), required=False)

            class Meta:
                model = Article
                fields = ['categories']

        category1 = Category.objects.create(name='cat1')
        category2 = Category.objects.create(name='cat2')
        article = Article.objects.create(
            pub_date=datetime.date(1988, 1, 4),
            writer=Writer.objects.create(name='Test writer'),
        )
        article.categories.set([category1.pk])

        form = ArticleForm(data={'categories': [category2.pk]}, instance=article)
        self.assertEqual(form.errors, {})
        self.assertEqual([x.pk for x in form.cleaned_data['categories']], [category2.pk])
        # Disabled fields use the value from `instance` rather than `data`.
        form = ArticleForm(data={'categories': [category2.pk]}, instance=article)
        form.fields['categories'].disabled = True
        self.assertEqual(form.errors, {})
        self.assertEqual([x.pk for x in form.cleaned_data['categories']], [category1.pk]) 
开发者ID:nesdis,项目名称:djongo,代码行数:26,代码来源:test_modelchoicefield.py

示例6: __init__

# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ModelMultipleChoiceField [as 别名]
def __init__(self, user=None, *args, **kwargs):

        super(FilterAnalysisForm, self).__init__(*args, **kwargs)

        if not user.is_authenticated:
            # print('user None', user)
            self.fields['individuals'] = forms.ModelMultipleChoiceField(queryset=Individual.objects.filter(user=None).order_by('id'), required=False, label='INDIVIDUALS')
            self.fields['exclude_individuals'] = forms.ModelMultipleChoiceField(queryset=Individual.objects.filter(user=None).order_by('id'), required=False, label='INDIVIDUALS')
        else:
            print('user', user)
            if user.is_superuser:
                self.fields['individuals'] = forms.ModelMultipleChoiceField(queryset=Individual.objects.filter().order_by('id'), required=False, label='INDIVIDUALS')
                self.fields['exclude_individuals'] = forms.ModelMultipleChoiceField(queryset=Individual.objects.filter().order_by('id'), required=False, label='INDIVIDUALS')
            else:
                self.fields['individuals'] = forms.ModelMultipleChoiceField(
                    queryset=Individual.objects.filter(Q(user=user) | Q(user=None)).order_by('id'), required=False, label='INDIVIDUALS')
                self.fields['exclude_individuals'] = forms.ModelMultipleChoiceField(
                    queryset=Individual.objects.filter(Q(user=user) | Q(user=None)).order_by('id'), required=False, label='INDIVIDUALS')
        # self.fields['sift'].widget.attrs['readonly'] = True
        # self.fields['polyphen'].widget.attrs['readonly'] = True
        # self.fields['genomes1000'].widget.attrs['readonly'] = True
        # self.fields['dbsnp_frequency'].widget.attrs['readonly'] = True
        # self.fields['esp_frequency'].widget.attrs['readonly'] = True
         

#trio analysis 
开发者ID:raonyguimaraes,项目名称:mendelmd,代码行数:28,代码来源:forms.py

示例7: formfield

# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ModelMultipleChoiceField [as 别名]
def formfield(self, **kwargs):
        db = kwargs.pop('using', None)
        defaults = {
            'form_class': forms.ModelMultipleChoiceField,
            'queryset': self.rel.to._default_manager.using(db),
        }
        defaults.update(kwargs)
        # If initial is passed in, it's a list of related objects, but the
        # MultipleChoiceField takes a list of IDs.
        if defaults.get('initial') is not None:
            initial = defaults['initial']
            if callable(initial):
                initial = initial()
            defaults['initial'] = [i._get_pk_val() for i in initial]
        return super(ManyToManyField, self).formfield(**defaults) 
开发者ID:lanbing510,项目名称:GTDWeb,代码行数:17,代码来源:related.py

示例8: __init__

# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ModelMultipleChoiceField [as 别名]
def __init__(self, project, *args, **kwargs):
        kwargs.setdefault('label_suffix', '')
        super(SiteBulkEditForm, self).__init__(*args, **kwargs)

        self.fields['sites'] = forms.ModelMultipleChoiceField(
            widget=forms.CheckboxSelectMultiple,
            queryset=project.sites.all(),
        )

        for attr in project.site_meta_attributes:
            q_type = attr['question_type']
            q_name = attr['question_name']

            if q_type == 'Number':
                field = forms.FloatField()
            elif q_type == 'Date':
                field = forms.DateField()
            elif q_type == 'MCQ':
                options = attr.get('mcq_options') or []
                choices = [o.get('option_text') for o in options]
                choices = [(c, c) for c in choices]
                field = forms.ChoiceField(choices=choices)
            else:
                field = forms.CharField()

            self.fields[q_name] = field 
开发者ID:awemulya,项目名称:kobo-predict,代码行数:28,代码来源:forms.py

示例9: formfield

# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ModelMultipleChoiceField [as 别名]
def formfield(self, *, using=None, **kwargs):
        defaults = {
            'form_class': forms.ModelMultipleChoiceField,
            'queryset': self.remote_field.model._default_manager.using(using),
        }
        defaults.update(kwargs)
        # If initial is passed in, it's a list of related objects, but the
        # MultipleChoiceField takes a list of IDs.
        if defaults.get('initial') is not None:
            initial = defaults['initial']
            if callable(initial):
                initial = initial()
            defaults['initial'] = [i.pk for i in initial]
        return super().formfield(**defaults) 
开发者ID:reBiocoder,项目名称:bioforum,代码行数:16,代码来源:related.py

示例10: __init__

# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ModelMultipleChoiceField [as 别名]
def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.helper = FormHelper()
        if settings.TARGET_PERMISSIONS_ONLY:
            self.common_layout = Layout('facility', 'target_id', 'observation_type')
        else:
            self.fields['groups'] = forms.ModelMultipleChoiceField(Group.objects.none(),
                                                                   required=False,
                                                                   widget=forms.CheckboxSelectMultiple)
            self.common_layout = Layout('facility', 'target_id', 'observation_type', 'groups')
        self.helper.layout = Layout(
            self.common_layout,
            self.layout(),
            self.button_layout()
        ) 
开发者ID:TOMToolkit,项目名称:tom_base,代码行数:17,代码来源:facility.py

示例11: __init__

# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ModelMultipleChoiceField [as 别名]
def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if not settings.TARGET_PERMISSIONS_ONLY:
            self.fields['groups'] = forms.ModelMultipleChoiceField(Group.objects.none(),
                                                                   required=False,
                                                                   widget=forms.CheckboxSelectMultiple) 
开发者ID:TOMToolkit,项目名称:tom_base,代码行数:8,代码来源:forms.py

示例12: __init__

# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ModelMultipleChoiceField [as 别名]
def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.fields['first_name'] = forms.ModelMultipleChoiceField(queryset=Student.objects.all())
        self.fields['first_name'].widget.attrs.update({'class': 'form-control'}) 
开发者ID:urankajtazaj,项目名称:SEMS,代码行数:6,代码来源:forms.py

示例13: formfield

# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ModelMultipleChoiceField [as 别名]
def formfield(self, *, using=None, **kwargs):
        defaults = {
            'form_class': forms.ModelMultipleChoiceField,
            'queryset': self.remote_field.model._default_manager.using(using),
            **kwargs,
        }
        # If initial is passed in, it's a list of related objects, but the
        # MultipleChoiceField takes a list of IDs.
        if defaults.get('initial') is not None:
            initial = defaults['initial']
            if callable(initial):
                initial = initial()
            defaults['initial'] = [i.pk for i in initial]
        return super().formfield(**defaults) 
开发者ID:PacktPublishing,项目名称:Hands-On-Application-Development-with-PyCharm,代码行数:16,代码来源:related.py

示例14: __init__

# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ModelMultipleChoiceField [as 别名]
def __init__(self, topic, *args, **kwargs):
        super(CommentMoveForm, self).__init__(*args, **kwargs)
        self.fields['comments'] = forms.ModelMultipleChoiceField(
            queryset=Comment.objects.filter(topic=topic),
            widget=forms.CheckboxSelectMultiple
        ) 
开发者ID:nitely,项目名称:Spirit,代码行数:8,代码来源:forms.py

示例15: formfield

# 需要导入模块: from django import forms [as 别名]
# 或者: from django.forms import ModelMultipleChoiceField [as 别名]
def formfield(self, **kwargs):
        db = kwargs.pop('using', None)
        defaults = {
            'form_class': forms.ModelMultipleChoiceField,
            'queryset': self.remote_field.model._default_manager.using(db),
        }
        defaults.update(kwargs)
        # If initial is passed in, it's a list of related objects, but the
        # MultipleChoiceField takes a list of IDs.
        if defaults.get('initial') is not None:
            initial = defaults['initial']
            if callable(initial):
                initial = initial()
            defaults['initial'] = [i._get_pk_val() for i in initial]
        return super(ManyToManyField, self).formfield(**defaults) 
开发者ID:Yeah-Kun,项目名称:python,代码行数:17,代码来源:related.py


注:本文中的django.forms.ModelMultipleChoiceField方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。