本文整理匯總了Python中django.forms.inlineformset_factory方法的典型用法代碼示例。如果您正苦於以下問題:Python forms.inlineformset_factory方法的具體用法?Python forms.inlineformset_factory怎麽用?Python forms.inlineformset_factory使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.forms
的用法示例。
在下文中一共展示了forms.inlineformset_factory方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_validate_inlineformset
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import inlineformset_factory [as 別名]
def test_validate_inlineformset(self):
FormSet = inlineformset_factory(Business, Employee, fields=('name', 'phone'))
business = Business.objects.create(name='Some Business')
emp_1 = Employee.objects.create(name='1', business=business, phone='4151112222')
formset = FormSet({
'employee_set-TOTAL_FORMS': '1',
'employee_set-INITIAL_FORMS': '2',
'employee_set-MIN_NUM_FORMS': '0',
'employee_set-MAX_NUM_FORMS': '1000',
'employee_set-0-name': 'Employee 1',
'employee_set-0-phone_0': '415 111 2222',
'employee_set-0-phone_1': '',
'employee_set-0-id': str(emp_1.pk),
'employee_set-0-business': str(business.pk)
}, instance=business)
formset.clean()
self.assertTrue(formset.is_valid())
示例2: question_change
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import inlineformset_factory [as 別名]
def question_change(request, quiz_pk, question_pk):
# Simlar to the `question_add` view, this view is also managing
# the permissions at object-level. By querying both `quiz` and
# `question` we are making sure only the owner of the quiz can
# change its details and also only questions that belongs to this
# specific quiz can be changed via this url (in cases where the
# user might have forged/player with the url params.
quiz = get_object_or_404(Quiz, pk=quiz_pk, owner=request.user)
question = get_object_or_404(Question, pk=question_pk, quiz=quiz)
AnswerFormSet = inlineformset_factory(
Question, # parent model
Answer, # base model
formset=BaseAnswerInlineFormSet,
fields=('text', 'is_correct'),
min_num=2,
validate_min=True,
max_num=10,
validate_max=True
)
if request.method == 'POST':
form = QuestionForm(request.POST, instance=question)
formset = AnswerFormSet(request.POST, instance=question)
if form.is_valid() and formset.is_valid():
with transaction.atomic():
form.save()
formset.save()
messages.success(request, 'Question and answers saved with success!')
return redirect('teachers:quiz_change', quiz.pk)
else:
form = QuestionForm(instance=question)
formset = AnswerFormSet(instance=question)
return render(request, 'classroom/teachers/question_change_form.html', {
'quiz': quiz,
'question': question,
'form': form,
'formset': formset
})
示例3: form_valid
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import inlineformset_factory [as 別名]
def form_valid(self, form):
self.blog_post = form.save(commit=False)
formset = inlineformset_factory(
Post, Post_Slugs, can_delete=True, extra=3, fields=('slug', 'is_active'),
formset=CustomBlogSlugInlineFormSet)
formset = formset(self.request.POST, instance=self.blog_post)
if not formset.is_valid():
return JsonResponse({'error': True, "response": formset.errors})
self.blog_post.user = self.request.user
if self.request.user.is_superuser:
self.blog_post.status = self.request.POST.get('status')
self.blog_post.save()
self.blog_post.store_old_slug(self.blog_post.slug)
formset.save()
if self.request.POST.get('tags', ''):
splitted = self.request.POST.get('tags').split(',')
for s in splitted:
blog_tags = Tags.objects.filter(name__iexact=s.strip())
if blog_tags:
blog_tag = blog_tags.first()
else:
blog_tag = Tags.objects.create(name=s.strip())
self.blog_post.tags.add(blog_tag)
self.blog_post.create_activity(user=self.request.user, content="added")
messages.success(self.request, 'Successfully posted your blog')
data = {'error': False, 'response': 'Successfully posted your blog',
'title': self.request.POST['title']}
return JsonResponse(data)
示例4: get_context_data
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import inlineformset_factory [as 別名]
def get_context_data(self, **kwargs):
context = super(PostCreateView, self).get_context_data(**kwargs)
tags_list = Tags.objects.all()
categories_list = Category.objects.filter(is_active=True)
context['status_choices'] = STATUS_CHOICE
context['categories_list'] = categories_list
context['tags_list'] = tags_list
context['add_blog'] = True
self.formset = inlineformset_factory(
Post, Post_Slugs, can_delete=True, extra=3, fields=('slug', 'is_active'),
formset=CustomBlogSlugInlineFormSet,
widgets={'slug': forms.TextInput(attrs={'class': 'form-control'})})
context['formset'] = self.formset()
return context
示例5: test_module_formset_required_for_publish
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import inlineformset_factory [as 別名]
def test_module_formset_required_for_publish(phase_factory):
class Form(forms.ModelForm):
class Meta:
model = phase_models.Phase
fields = ['start_date']
required_for_project_publish = ['start_date']
widgets = {'start_date': forms.TextInput()}
FormSet = inlineformset_factory(module_models.Module,
phase_models.Phase,
form=Form,
formset=ModuleDashboardFormSet,
extra=0,
can_delete=False,
)
phase = phase_factory(module__project__is_draft=True)
module = phase.module
project = module.project
phase_factory(module=module)
formset = FormSet(instance=module)
assert formset.get_required_fields() == ['start_date']
assert all(map(lambda field: field.required is False,
[form.fields['start_date'] for form in formset.forms]))
project.is_draft = False
formset = FormSet(instance=module)
assert formset.get_required_fields() == ['start_date']
assert all(map(lambda field: field.required is True,
[form.fields['start_date'] for form in formset.forms]))
示例6: question_change
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import inlineformset_factory [as 別名]
def question_change(request, quiz_pk, question_pk):
quiz = get_object_or_404(Quiz, pk=quiz_pk, owner=request.user)
question = get_object_or_404(Question, pk=question_pk, quiz=quiz)
AnswerFormSet = inlineformset_factory(
Question,
Answer,
formset = BaseAnswerInlineFormSet,
fields = ('text', 'is_correct'),
min_num = 2,
validate_min = True,
max_num = 10,
validate_max = True
)
if request.method == 'POST':
form = QuestionForm(request.POST, instance=question)
formset = AnswerFormSet(request.POST, instance=question)
if form.is_valid() and formset.is_valid():
with transaction.atomic():
form.save()
formset.save()
messages.success(request, 'Question and answers saved with success!')
return redirect('teacher_update_quiz', quiz.pk)
else:
form = QuestionForm(instance=question)
formset = AnswerFormSet(instance=question)
return render(request, 'students/teacher/question_change_form.html', {
'quiz': quiz,
'question': question,
'form': form,
'formset': formset
})
示例7: test_inlineformset_pass_locales_down
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import inlineformset_factory [as 別名]
def test_inlineformset_pass_locales_down():
a = Author.objects.create(name='Tolkien')
title = 'The Lord of the Rings'
abstract = 'Frodo tries to destroy a ring'
Book.objects.create(author=a, title=title, abstract=abstract)
FormSetClass = inlineformset_factory(Author, Book, form=BookForm, formset=I18nInlineFormSet)
fs = FormSetClass(locales=['de', 'fr'], instance=a)
assert fs.forms[0].fields['title'].widget.enabled_locales == ['de', 'fr']
assert fs.empty_form.fields['title'].widget.enabled_locales == ['de', 'fr']
示例8: test_module_formset_component
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import inlineformset_factory [as 別名]
def test_module_formset_component(phase_factory):
class Form(forms.ModelForm):
class Meta:
model = phase_models.Phase
fields = ['start_date']
required_for_project_publish = ['start_date']
widgets = {'start_date': forms.TextInput()}
FormSet = inlineformset_factory(module_models.Module,
phase_models.Phase,
form=Form,
formset=ModuleDashboardFormSet,
extra=0,
can_delete=False,
)
class Component(ModuleFormSetComponent):
identifier = 'test_id'
weight = 1
label = 'Module Settings'
form_title = 'Edit test module settings'
form_class = FormSet
form_template_name = 'none'
phase = phase_factory(module__project__is_draft=True,
start_date=None)
module = phase.module
phase_factory(module=module, start_date=None)
component = Component()
assert component.is_effective(module) is True
urls = component.get_urls()
assert len(urls) == 1
regexp, _, name = urls[0]
assert regexp == r'^modules/(?P<module_slug>[-\w_]+)/test_id/$'
assert name == 'dashboard-test_id-edit'
assert component.get_progress(module) == (0, 2)
phase.start_date = now()
phase.save()
assert component.get_progress(module) == (1, 2)