本文整理匯總了Python中django.forms.modelform_factory方法的典型用法代碼示例。如果您正苦於以下問題:Python forms.modelform_factory方法的具體用法?Python forms.modelform_factory怎麽用?Python forms.modelform_factory使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.forms
的用法示例。
在下文中一共展示了forms.modelform_factory方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: create_classes
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import modelform_factory [as 別名]
def create_classes(request):
""" Create classes view """
ClassFormSet = modelform_factory(
Semester_Class,
fields=("semester", "department", "class_number", "class_section"),
)
if request.method == "POST":
formset = ClassFormSet(request.POST)
if formset.is_valid():
stock = formset.save(commit=False)
stock.professor_id = request.user
stock.save()
messages.success(request, f"Class Added")
return redirect("profile")
else:
formset = ClassFormSet()
return render(
request,
"gatorgrouper/classes.html",
{"title": "Create Classes", "formset": formset},
)
# Allows the user to view the list of assignments
示例2: setUp
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import modelform_factory [as 別名]
def setUp(self):
self.CalendarForm = modelform_factory(Calendar, fields=("name",))
示例3: get_form
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import modelform_factory [as 別名]
def get_form(self):
config_models = [
model for model in apps.get_models() if issubclass(model, ByroConfiguration)
]
data = self.request.POST if self.request.method == "POST" else None
return [
forms.modelform_factory(
model, fields="__all__", exclude=("registration_form",)
)(prefix=model.__name__, instance=model.get_solo(), data=data)
for model in config_models
]
示例4: get_form_class
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import modelform_factory [as 別名]
def get_form_class(self):
return modelform_factory(
self.model,
form=ParticipantForm,
fields=['name', 'vip', 'email', 'phone_number', 'biography', 'notes'] + ParticipantForm.SOCIAL_FIELDS,
)
示例5: widget_for_object_field
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import modelform_factory [as 別名]
def widget_for_object_field(obj, field_name):
FieldForm = modelform_factory(
obj.source_dynmodel().get_model(),
fields=(field_name,)
)
widget = FieldForm().fields[field_name].widget
return widget
示例6: get_form_class
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import modelform_factory [as 別名]
def get_form_class(self):
fields = self.object.settings['fields']
form_class = modelform_factory(self.model, fields=fields)
return form_class
示例7: get_page_form
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import modelform_factory [as 別名]
def get_page_form(model_ct, form=HelpBasePageForm):
model = model_ct.model_class()
assert issubclass(model, models.HelpBasePage) and model != models.HelpBasePage, \
"Model must be a subclass of HelpBasePage, but not HelpBasePage itself."
assert model in PAGE_TYPES, "Not a supported model"
form = forms.modelform_factory(model, form=form, fields=model.admin_fields)
form.model_ct = model_ct
return form
示例8: _get_model_form_class
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import modelform_factory [as 別名]
def _get_model_form_class(model_form_class, model_container, admin, request):
if not model_form_class:
form_kwargs = dict(
form=forms.ModelForm,
fields=forms.ALL_FIELDS,
)
if admin and request:
form_kwargs['formfield_callback'] = functools.partial(
admin.formfield_for_dbfield, request=request)
model_form_class = modelform_factory(model_container, **form_kwargs)
return model_form_class
示例9: test_modelform_rendering
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import modelform_factory [as 別名]
def test_modelform_rendering(self):
Form = modelform_factory(TestModel, fields=('phone',))
obj = TestModel(phone='415 123 4567 x 88')
f = Form(instance=obj)
self.assertEqual(str(f), _rendered_field_html(phone_number='(415) 123-4567', extension='88', required=True))
示例10: test_modelform_rendering_nullable
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import modelform_factory [as 別名]
def test_modelform_rendering_nullable(self):
Form = modelform_factory(TestModelBlankNull, fields=('phone',))
obj = TestModel(phone='415 123 4567 x 88')
f = Form(instance=obj)
self.assertEqual(str(f), _rendered_field_html(phone_number='(415) 123-4567', extension='88', required=False))
示例11: test_modelform_saving
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import modelform_factory [as 別名]
def test_modelform_saving(self):
Form = modelform_factory(TestModel, fields=('phone',))
f = Form({'phone_0': '415.123.4567', 'phone_1': '88'})
self.assertTrue(f.is_valid())
obj = f.save()
self.assertIsInstance(obj.phone, PhoneNumber)
self.assertEqual(str(obj.phone), '(415) 123-4567, press 88')
示例12: test_modelform_empty
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import modelform_factory [as 別名]
def test_modelform_empty(self):
Form = modelform_factory(TestModelOptional, fields=('name', 'phone'))
f = Form({'name': 'Ted', 'phone_0': '', 'phone_1': ''})
self.assertTrue(f.is_valid())
obj = f.save()
self.assertEqual(obj.phone, '')
示例13: get_form_class
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import modelform_factory [as 別名]
def get_form_class(self):
return modelform_factory(self.model, form=AddNotificationForm)
示例14: get_forms
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import modelform_factory [as 別名]
def get_forms(self):
obj = self.get_object()
membership_create_form = forms.modelform_factory(
Membership, fields=["start", "end", "interval", "amount"]
)
for key in membership_create_form.base_fields:
setattr(membership_create_form.base_fields[key], "required", False)
return (
[
self._instantiate(
forms.modelform_factory(Member, exclude=["membership_type"]),
member=obj,
instance=obj,
)
]
+ [
self._instantiate(
forms.modelform_factory(Membership, exclude=["member"]),
member=obj,
instance=m,
prefix=m.id,
)
for m in obj.memberships.all()
]
+ [
self._instantiate(
membership_create_form,
member=obj,
profile_class=Membership,
empty=True,
)
]
+ [
self._instantiate(
forms.modelform_factory(
profile_class,
fields=[
f.name
for f in profile_class._meta.fields
if f.name not in ["id", "member"]
],
),
member=obj,
profile_class=profile_class,
)
for profile_class in obj.profile_classes
]
)
示例15: get_form_class
# 需要導入模塊: from django import forms [as 別名]
# 或者: from django.forms import modelform_factory [as 別名]
def get_form_class(self):
# We want to have different fields for different comrades, but
# self.fields is shared across all instances of this view, so we can't
# use that. There's no get_fields() method we can override, either, so
# the only hook we can use is overriding this method of ModelFormMixin.
fields = [
'public_name',
'nick_name',
'legal_name',
'pronouns',
'pronouns_to_participants',
'pronouns_public',
]
comrade = self.object
# was an approved coordinator for a community that had at least one approved participation
coordinatored = comrade.coordinatorapproval_set.approved().filter(
community__participation__approval_status=ApprovalStatus.APPROVED,
)
# was an approved mentor for some approved project in an approved community
mentored = comrade.get_mentored_projects().approved().filter(
project_round__approval_status=ApprovalStatus.APPROVED,
)
# was an approved application reviewer at some point
reviewered = comrade.applicationreviewer_set.approved()
# people who participated in some way at some time can set a photo of themselves
if comrade.account.is_staff or comrade.get_intern_selection() is not None or coordinatored.exists() or mentored.exists() or reviewered.exists():
fields.append('photo')
fields.extend([
'timezone',
'location',
'nick',
'github_url',
'gitlab_url',
'blog_url',
'blog_rss_url',
'twitter_url',
'agreed_to_code_of_conduct',
])
return modelform_factory(comrade.__class__, fields=fields)