本文整理汇总了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)