本文整理匯總了Python中django.forms.formset_factory方法的典型用法代碼示例。如果您正苦於以下問題:Python forms.formset_factory方法的具體用法?Python forms.formset_factory怎麽用?Python forms.formset_factory使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.forms
的用法示例。
在下文中一共展示了forms.formset_factory方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_clean
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import formset_factory [as 別名]
def test_clean(self):
ContactFormset = formset_factory(form=ContactsForm)
value = ContactFormset({
'form-TOTAL_FORMS': '2',
'form-INITIAL_FORMS': '1',
'form-MIN_NUM_FORMS': '0',
'form-MAX_NUM_FORMS': '1000',
'form-0-name': 'Ringo',
'form-0-email': 'ringo@beatles.uk',
'form-0-tel': '555-555',
'form-1-name': 'john',
'form-1-email': 'john@beatles.uk',
'form-1-tel': '555-555',
'form-1-remove': True,
'form-2-name': '',
'form-2-email': '',
'form-2-tel': '',
})
field = org_fields.ContactsField(form=ContactsForm)
expected = [{
'name': 'Ringo',
'email': 'ringo@beatles.uk',
'tel': '555-555'
}]
assert field.clean(value) == expected
示例2: test_invalid_clean
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import formset_factory [as 別名]
def test_invalid_clean(self):
ContactFormset = formset_factory(form=ContactsForm)
value = ContactFormset({
'form-TOTAL_FORMS': '2',
'form-INITIAL_FORMS': '1',
'form-MIN_NUM_FORMS': '0',
'form-MAX_NUM_FORMS': '1000',
'form-0-name': 'Ringo',
'form-0-email': 'ringo@beatles',
'form-0-tel': '',
'form-1-name': '',
'form-1-email': '',
'form-1-tel': ''
})
field = org_fields.ContactsField(form=ContactsForm)
with raises(ValidationError):
field.clean(value)
示例3: test_value_from_datadict
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import formset_factory [as 別名]
def test_value_from_datadict(self):
formset = formset_factory(ContactsForm)
widget = widgets.ContactsWidget(attrs={'formset': formset})
data = {
'contacts-TOTAL_FORMS': '2',
'contacts-INITIAL_FORMS': '1',
'contacts-MIN_NUM_FORMS': '0',
'contacts-MAX_NUM_FORMS': '1000',
'contacts-0-name': 'Ringo',
'contacts-0-email': 'ringo@beatles.uk',
'contacts-0-tel': '555-555',
'contacts-1-name': '',
'contacts-1-email': '',
'contacts-1-tel': ''
}
q_dict = QueryDict('', mutable=True)
q_dict.update(data)
value = widget.value_from_datadict(q_dict, {}, 'contacts')
assert isinstance(value, formset)
示例4: post
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import formset_factory [as 別名]
def post(self, request, *args, **kwargs):
search_ids = list(set(request.POST.get('search_id').split()))
project_pk = self.kwargs.get('project_pk')
project_obj = get_object_or_404(Project, pk=project_pk)
pubs = []
for ele in search_ids:
pub_dict = self._search_id(ele)
if pub_dict:
pubs.append(pub_dict)
formset = formset_factory(PublicationResultForm, max_num=len(pubs))
formset = formset(initial=pubs, prefix='pubform')
context = {}
context['project_pk'] = project_obj.pk
context['formset'] = formset
context['search_ids'] = search_ids
context['pubs'] = pubs
return render(request, self.template_name, context)
示例5: get
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import formset_factory [as 別名]
def get(self, request, *args, **kwargs):
pk = self.kwargs.get('pk')
allocation_obj = get_object_or_404(Allocation, pk=pk)
allocation_attributes_to_delete = self.get_allocation_attributes_to_delete(
allocation_obj)
context = {}
if allocation_attributes_to_delete:
formset = formset_factory(AllocationAttributeDeleteForm, max_num=len(
allocation_attributes_to_delete))
formset = formset(
initial=allocation_attributes_to_delete, prefix='attributeform')
context['formset'] = formset
context['allocation'] = allocation_obj
return render(request, self.template_name, context)
示例6: __init__
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import formset_factory [as 別名]
def __init__(self, name, model_form_class, model_container, mdl_form_kw_l,
widget=None, admin=None, request=None, *args, **kwargs):
self.name = name
self.model_container = model_container
self.model_form_class = _get_model_form_class(
model_form_class, model_container, admin, request)
self.mdl_form_kw_l = mdl_form_kw_l
self.admin = admin
self.request = request
if not widget:
widget = ArrayFormWidget(self.model_form_class.__name__)
error_messages = {
'incomplete': 'Enter all required fields.',
}
self.ArrayFormSet = forms.formset_factory(
self.model_form_class, formset=NestedFormSet, can_delete=True)
super().__init__(error_messages=error_messages,
widget=widget, *args, **kwargs)
示例7: _create_formset
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import formset_factory [as 別名]
def _create_formset(self, extra=0):
from django.forms import formset_factory
# from django.forms import BaseFormSet
FormSet = formset_factory(self.get_form_class(), extra=extra)
formset = FormSet(form_kwargs=self.get_form_kwargs())
return formset
示例8: __init__
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import formset_factory [as 別名]
def __init__(self, form=None, *args, **kwargs):
self.formset = forms.formset_factory(form)
super().__init__(*args, **kwargs)
示例9: test_init
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import formset_factory [as 別名]
def test_init(self):
field = org_fields.ContactsField(form=ContactsForm)
assert isinstance(field.formset, type(formset_factory(ContactsForm)))
示例10: get
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import formset_factory [as 別名]
def get(self, request, *args, **kwargs):
pk = self.kwargs.get('pk')
project_obj = get_object_or_404(Project, pk=pk)
users_to_remove = self.get_users_to_remove(project_obj)
context = {}
if users_to_remove:
formset = formset_factory(
ProjectRemoveUserForm, max_num=len(users_to_remove))
formset = formset(initial=users_to_remove, prefix='userform')
context['formset'] = formset
context['project'] = get_object_or_404(Project, pk=pk)
return render(request, self.template_name, context)
示例11: get
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import formset_factory [as 別名]
def get(self, request, *args, **kwargs):
project_obj = get_object_or_404(Project, pk=self.kwargs.get('project_pk'))
grants_to_delete = self.get_grants_to_delete(project_obj)
context = {}
if grants_to_delete:
formset = formset_factory(GrantDeleteForm, max_num=len(grants_to_delete))
formset = formset(initial=grants_to_delete, prefix='grantform')
context['formset'] = formset
context['project'] = project_obj
return render(request, self.template_name, context)
示例12: post
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import formset_factory [as 別名]
def post(self, request, *args, **kwargs):
project_obj = get_object_or_404(Project, pk=self.kwargs.get('project_pk'))
grants_to_delete = self.get_grants_to_delete(project_obj)
context = {}
formset = formset_factory(GrantDeleteForm, max_num=len(grants_to_delete))
formset = formset(request.POST, initial=grants_to_delete, prefix='grantform')
grants_deleted_count = 0
if formset.is_valid():
for form in formset:
grant_form_data = form.cleaned_data
if grant_form_data['selected']:
grant_obj = Grant.objects.get(
project=project_obj,
title=grant_form_data.get('title'),
grant_number=grant_form_data.get('grant_number')
)
grant_obj.delete()
grants_deleted_count += 1
messages.success(request, 'Deleted {} grants from project.'.format(grants_deleted_count))
else:
for error in formset.errors:
messages.error(request, error)
return HttpResponseRedirect(reverse('project-detail', kwargs={'pk': project_obj.pk}))
示例13: post
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import formset_factory [as 別名]
def post(self, request, *args, **kwargs):
pk = self.kwargs.get('pk')
allocation_obj = get_object_or_404(Allocation, pk=pk)
users_to_remove = self.get_users_to_remove(allocation_obj)
formset = formset_factory(
AllocationRemoveUserForm, max_num=len(users_to_remove))
formset = formset(
request.POST, initial=users_to_remove, prefix='userform')
remove_users_count = 0
if formset.is_valid():
allocation_user_removed_status_choice = AllocationUserStatusChoice.objects.get(
name='Removed')
for form in formset:
user_form_data = form.cleaned_data
if user_form_data['selected']:
remove_users_count += 1
user_obj = User.objects.get(
username=user_form_data.get('username'))
if allocation_obj.project.pi == user_obj:
continue
allocation_user_obj = allocation_obj.allocationuser_set.get(
user=user_obj)
allocation_user_obj.status = allocation_user_removed_status_choice
allocation_user_obj.save()
allocation_remove_user.send(sender=self.__class__,
allocation_user_pk=allocation_user_obj.pk)
messages.success(
request, 'Removed {} users from allocation.'.format(remove_users_count))
else:
for error in formset.errors:
messages.error(request, error)
return HttpResponseRedirect(reverse('allocation-detail', kwargs={'pk': pk}))
示例14: ProductsForm
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import formset_factory [as 別名]
def ProductsForm(category, products):
''' Produces an appropriate _ProductsForm subclass for the given render
type. '''
# Each Category.RENDER_TYPE value has a subclass here.
cat = inventory.Category
RENDER_TYPES = {
cat.RENDER_TYPE_QUANTITY: _QuantityBoxProductsForm,
cat.RENDER_TYPE_RADIO: _RadioButtonProductsForm,
cat.RENDER_TYPE_ITEM_QUANTITY: _ItemQuantityProductsForm,
cat.RENDER_TYPE_CHECKBOX: _CheckboxProductsForm,
}
# Produce a subclass of _ProductsForm which we can alter the base_fields on
class ProductsForm(RENDER_TYPES[category.render_type]):
pass
products = list(products)
products.sort(key=lambda prod: prod.order)
ProductsForm.set_fields(category, products)
if category.render_type == inventory.Category.RENDER_TYPE_ITEM_QUANTITY:
ProductsForm = forms.formset_factory(
ProductsForm,
formset=_ItemQuantityProductsFormSet,
)
return ProductsForm
示例15: staff_products_formset_factory
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import formset_factory [as 別名]
def staff_products_formset_factory(user):
''' Creates a formset of StaffProductsForm for the given user. '''
form_type = staff_products_form_factory(user)
return forms.formset_factory(form_type)