本文整理匯總了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)
示例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')]
示例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())
示例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])
示例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])
示例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
示例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)
示例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
示例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)
示例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()
)
示例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)
示例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'})
示例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)
示例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
)
示例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)