當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。