当前位置: 首页>>代码示例>>Python>>正文


Python ModelForm.is_valid方法代码示例

本文整理汇总了Python中django.forms.ModelForm.is_valid方法的典型用法代码示例。如果您正苦于以下问题:Python ModelForm.is_valid方法的具体用法?Python ModelForm.is_valid怎么用?Python ModelForm.is_valid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.forms.ModelForm的用法示例。


在下文中一共展示了ModelForm.is_valid方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: is_valid

# 需要导入模块: from django.forms import ModelForm [as 别名]
# 或者: from django.forms.ModelForm import is_valid [as 别名]
	def is_valid(self):
		if ModelForm.is_valid(self):	
			programm = L_get_left_programm(self.instance.year, self.instance.semester, self.instance.id)
			if programm[self.instance.course][self.instance.course_number][self.instance.course_type] < self.instance.num_of_groups:
				return False
		else:
			return False
		return True
开发者ID:SgnJp,项目名称:hlc,代码行数:10,代码来源:models.py

示例2: register_view

# 需要导入模块: from django.forms import ModelForm [as 别名]
# 或者: from django.forms.ModelForm import is_valid [as 别名]
    def register_view(self, request, form_url='', extra_context=None):
        request.user = STAFF_REGISTER
        ModelForm = self.get_form(request, None)

        if request.method == 'POST':
            form = ModelForm(request.POST, request.FILES, instance=None)
            if form.is_valid():
                form_validated = True
                new_object = self.save_form(request, form, change=False)
            else:
                form_validated = False
                new_object = form.instance

            formsets, inline_instances = self._create_formsets(request, new_object)
            if all_valid(formsets) and form_validated:
                self.save_model(request, new_object, form, False)

            self.log_addition(request, new_object)
            return self.response_add(request, new_object, post_url_continue='/management/login/')
        else:
            initial = self.get_changeform_initial_data(request)
            form = ModelForm(initial=initial)
            formsets, inline_instances = self._create_formsets(request, self.model())

            adminForm = helpers.AdminForm(form,
                                          list(self.get_fieldsets(request, None)),
                                          self.get_prepopulated_fields(request, None),
                                          self.get_readonly_fields(request, None),
                                          model_admin=self)
            media = self.media + adminForm.media

            inline_formsets = self.get_inline_formsets(request, formsets, inline_instances, None)
            for inline_formset in inline_formsets:
                media = media + inline_formset.media

            context = dict(self.admin_site.each_context(),
                           title=u'注册%s' % force_text(self.model._meta.verbose_name),
                           adminform=adminForm,
                           object_id=None,
                           original=None,
                           is_popup=(IS_POPUP_VAR in request.POST or
                                     IS_POPUP_VAR in request.GET),
                           to_field=None,
                           media=media,
                           # formsets=formsets,
                           inline_admin_formsets=inline_formsets,
                           errors=helpers.AdminErrorList(form, formsets),
                           preserved_filters=self.get_preserved_filters(request))

            context.update(extra_context or {})
            return self.render_change_form(request,
                                           context,
                                           add=True,
                                           change=False,
                                           obj=None,
                                           form_url=form_url)
开发者ID:luotao-net,项目名称:persimmon,代码行数:58,代码来源:admin.py

示例3: _is_valid

# 需要导入模块: from django.forms import ModelForm [as 别名]
# 或者: from django.forms.ModelForm import is_valid [as 别名]
 def _is_valid(self):
     res = ModelForm.is_valid(self)
     if self.instance.id is None:
         return res
     for f in FlagType.objects.all():
         fv = self.data[f.name]
         d = {'item': self.instance.id, 'type': f.id, 'value': fv}
         try:
             fg = Flag.objects.get(item=self.instance.id, type=f.id)
         except Flag.DoesNotExist:
             continue
         ff = FlagForm(d, instance=fg)
         if not ff.is_valid():
             res = False
             self.errors[f.name] = ff.errors
     return res
开发者ID:FreakTheMighty,项目名称:pyjamas,代码行数:18,代码来源:forms.py

示例4: add_view

# 需要导入模块: from django.forms import ModelForm [as 别名]
# 或者: from django.forms.ModelForm import is_valid [as 别名]
    def add_view(self, request, form_url='', extra_context=None):
        "The 'add' admin view for this model."
        model = self.model
        opts = model._meta

        if not self.has_add_permission(request):
            raise PermissionDenied

        ModelForm = self.get_form(request)
        formsets = []
        if request.method == 'POST':
            form = ModelForm(request.POST, request.FILES)
            if form.is_valid():
                form_validated = True
                new_object = self.save_form(request, form, change=False)
            else:
                form_validated = False
                new_object = self.model()
            prefixes = {}
            for FormSet in self.get_formsets(request):
                prefix = FormSet.get_default_prefix()
                prefixes[prefix] = prefixes.get(prefix, 0) + 1
                if prefixes[prefix] != 1:
                    prefix = "%s-%s" % (prefix, prefixes[prefix])
                formset = FormSet(data=request.POST, files=request.FILES,
                                  instance=new_object,
                                  save_as_new=request.POST.has_key("_saveasnew"),
                                  prefix=prefix)
                formsets.append(formset)
            if all_valid(formsets) and form_validated:
                # Here is the modified code.
                for formset, inline in zip(formsets, self.inline_instances):
                    if not isinstance(inline, ReverseInlineModelAdmin):
                        continue
                    obj = formset.save()[0]
                    setattr(new_object, inline.parent_fk_name, obj)
                self.save_model(request, new_object, form, change=False)
                form.save_m2m()
                for formset in formsets:
                    self.save_formset(request, form, formset, change=False)

                self.log_addition(request, new_object)
                return self.response_add(request, new_object)
        else:
            # Prepare the dict of initial data from the request.
            # We have to special-case M2Ms as a list of comma-separated PKs.
            initial = dict(request.GET.items())
            for k in initial:
                try:
                    f = opts.get_field(k)
                except models.FieldDoesNotExist:
                    continue
                if isinstance(f, models.ManyToManyField):
                    initial[k] = initial[k].split(",")
            form = ModelForm(initial=initial)
            prefixes = {}
            for FormSet in self.get_formsets(request):
                prefix = FormSet.get_default_prefix()
                prefixes[prefix] = prefixes.get(prefix, 0) + 1
                if prefixes[prefix] != 1:
                    prefix = "%s-%s" % (prefix, prefixes[prefix])
                formset = FormSet(instance=self.model(), prefix=prefix)
                formsets.append(formset)

        adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)), self.prepopulated_fields)
        media = self.media + adminForm.media

        inline_admin_formsets = []
        for inline, formset in zip(self.inline_instances, formsets):
            fieldsets = list(inline.get_fieldsets(request))
            inline_admin_formset = helpers.InlineAdminFormSet(inline, formset, fieldsets)
            inline_admin_formsets.append(inline_admin_formset)
            media = media + inline_admin_formset.media

        context = {
            'title': _('Add %s') % force_unicode(opts.verbose_name),
            'adminform': adminForm,
            'is_popup': request.REQUEST.has_key('_popup'),
            'show_delete': False,
            'media': mark_safe(media),
            'inline_admin_formsets': inline_admin_formsets,
            'errors': helpers.AdminErrorList(form, formsets),
            #'root_path': self.admin_site.root_path,
            'app_label': opts.app_label,
        }
        context.update(extra_context or {})
        return self.render_change_form(request, context, form_url=form_url, add=True)
开发者ID:ozgurgunes,项目名称:django-filizver,代码行数:89,代码来源:admin.py

示例5: change_view

# 需要导入模块: from django.forms import ModelForm [as 别名]
# 或者: from django.forms.ModelForm import is_valid [as 别名]
	def change_view(self, request, object_id, form_url='', extra_context=None):
		"The 'change' admin view for this model."
		model = self.model
		opts = model._meta

		obj = self.get_object(request, unquote(object_id))

		if not self.has_change_permission(request, obj):

			raise PermissionDenied

		if obj is None:
			from django.http import Http404
			from django.utils.html import escape
			raise Http404(_('%(name)s object with primary key %(key)r does not exist.') % {'name': force_text(opts.verbose_name), 'key': escape(object_id)})

		if request.method == 'POST' and "_saveasnew" in request.POST:
			return self.add_view(request, form_url=reverse('admin:%s_%s_add' % (
				opts.app_label, opts.model_name),
				current_app=self.admin_site.name))

		ModelForm = self.get_form(request, obj)
		formsets = []
		if request.method == 'POST':
			form = ModelForm(request.POST, request.FILES, instance=obj)
			if form.is_valid():
				form_validated = True
				new_object = self.save_form(request, form, change=True)
			else:
				form_validated = False
				new_object = form.instance

			formsets, inline_instances = self._create_formsets(request, new_object)
			if all_valid(formsets) and form_validated:
				self.save_model(request, new_object, form, True)
				self.save_related(request, form, formsets, True)
				change_message = self.construct_change_message(request, form, formsets)
				self.log_change(request, new_object, change_message)
				return self.response_change(request, new_object)

		else:
			form = ModelForm(instance=obj)
			formsets, inline_instances = self._create_formsets(request, obj)

		adminForm = helpers.AdminForm(form, self.get_fieldsets(request, obj),
			self.get_prepopulated_fields(request, obj),
			self.get_readonly_fields(request, obj),
			model_admin=self)
		media = self.media + adminForm.media

		inline_admin_formsets = []
		for inline, formset in zip(inline_instances, formsets):
			fieldsets = list(inline.get_fieldsets(request, obj))
			readonly = list(inline.get_readonly_fields(request, obj))
			prepopulated = dict(inline.get_prepopulated_fields(request, obj))
			inline_admin_formset = helpers.InlineAdminFormSet(inline, formset,
				fieldsets, prepopulated, readonly, model_admin=self)
			inline_admin_formsets.append(inline_admin_formset)
			media = media + inline_admin_formset.media

		context = dict(self.admin_site.each_context(),
			title=_('Change %s') % force_text(opts.verbose_name),
			adminform=adminForm,
			object_id=object_id,
			original=obj,
			is_popup=(IS_POPUP_VAR in request.POST or
					  IS_POPUP_VAR in request.GET),
			to_field=request.POST.get(TO_FIELD_VAR,
									  request.GET.get(TO_FIELD_VAR)),
			media=media,
			inline_admin_formsets=inline_admin_formsets,
			errors=helpers.AdminErrorList(form, formsets),
			app_label=opts.app_label,
			preserved_filters=self.get_preserved_filters(request),
		)
		context.update(extra_context or {})
		return self.render_change_form(request, context, change=True, obj=obj, form_url=form_url)
开发者ID:CICIC,项目名称:gestioCI_butterfly_release,代码行数:79,代码来源:reverseadmin.py

示例6: add_view

# 需要导入模块: from django.forms import ModelForm [as 别名]
# 或者: from django.forms.ModelForm import is_valid [as 别名]
	def add_view(self, request, form_url='', extra_context=None):
		"The 'add' admin view for this model."
		model = self.model
		opts = model._meta
		if not self.has_add_permission(request):
			raise PermissionDenied

		ModelForm = self.get_form(request)
		formsets = []
		if request.method == 'POST':
			form = ModelForm(request.POST, request.FILES)
			if form.is_valid():
				form_validated = True
				new_object = self.save_form(request, form, change=False)
			else:
				form_validated = False
				new_object = self.model()
			prefixes = {}
			FormSets = []
			formsets, inline_instances = self._create_formsets(request, new_object)
			if all_valid(formsets) and form_validated:
				# Here is the modified code.
				for formset, inline in zip(formsets, self.inline_instances):
					if not isinstance(inline, ReverseInlineModelAdmin):
						continue
					obj = formset.save()[0]
					setattr(new_object, inline.parent_fk_name, obj)
				self.save_model(request, new_object, form, change=False)
				form.save_m2m()
				for formset in formsets:
					self.save_formset(request, form, formset, change=False)

				self.log_addition(request, new_object)
				return self.response_add(request, new_object)
		else:
			# Prepare the dict of initial data from the request.
			# We have to special-case M2Ms as a list of comma-separated PKs.
			initial = dict(request.GET.items())
			for k in initial:
				try:
					f = opts.get_field(k)
				except FieldDoesNotExist:
					continue
				if isinstance(f, models.ManyToManyField):
					initial[k] = initial[k].split(",")
			form = ModelForm(initial=initial)
			formsets, inline_instances = self._create_formsets(request, self.model())

		adminForm = helpers.AdminForm(form, list(self.get_fieldsets(request)),
			self.get_prepopulated_fields(request),
			self.get_readonly_fields(request),
			model_admin=self)
		media = self.media + adminForm.media

		inline_admin_formsets = []
		for inline, formset in zip(inline_instances, formsets):
			fieldsets = list(inline.get_fieldsets(request))
			readonly = list(inline.get_readonly_fields(request))
			prepopulated = dict(inline.get_prepopulated_fields(request))
			inline_admin_formset = helpers.InlineAdminFormSet(inline, formset,
				fieldsets, prepopulated, readonly, model_admin=self)
			inline_admin_formsets.append(inline_admin_formset)
			media = media + inline_admin_formset.media

		context = dict(self.admin_site.each_context(),
			title=_('Add %s') % force_text(opts.verbose_name),
			adminform=adminForm,
			is_popup=(IS_POPUP_VAR in request.POST or
					  IS_POPUP_VAR in request.GET),
			to_field=request.POST.get(TO_FIELD_VAR,
									  request.GET.get(TO_FIELD_VAR)),
			media=media,
			inline_admin_formsets=inline_admin_formsets,
			errors=helpers.AdminErrorList(form, formsets),
			app_label=opts.app_label,
			preserved_filters=self.get_preserved_filters(request),
		)
		context.update(extra_context or {})
		return self.render_change_form(request, context, form_url=form_url, add=True)
开发者ID:CICIC,项目名称:gestioCI_butterfly_release,代码行数:81,代码来源:reverseadmin.py

示例7: changeform_view

# 需要导入模块: from django.forms import ModelForm [as 别名]
# 或者: from django.forms.ModelForm import is_valid [as 别名]
    def changeform_view(self, request, object_id=None, form_url="", extra_context=None):

        to_field = request.POST.get(TO_FIELD_VAR, request.GET.get(TO_FIELD_VAR))
        if to_field and not self.to_field_allowed(request, to_field):
            raise DisallowedModelAdminToField("The field %s cannot be referenced." % to_field)

        model = self.model
        opts = model._meta
        add = object_id is None

        if add:
            if not self.has_add_permission(request):
                raise PermissionDenied
            obj = None

        else:
            obj = self.get_object(request, unquote(object_id), to_field)

            if not self.has_change_permission(request, obj):
                raise PermissionDenied

            if obj is None:
                raise Http404(
                    _("%(name)s object with primary key %(key)r does not exist.")
                    % {"name": force_text(opts.verbose_name), "key": escape(object_id)}
                )

            if request.method == "POST" and "_saveasnew" in request.POST:
                return self.add_view(
                    request,
                    form_url=reverse(
                        "admin:%s_%s_add" % (opts.app_label, opts.model_name), current_app=self.admin_site.name
                    ),
                )

        ModelForm = self.get_form(request, obj)
        if request.method == "POST":
            form = ModelForm(request.POST, request.FILES, instance=obj)
            if form.is_valid():
                form_validated = True
                new_object = self.save_form(request, form, change=not add)
            else:
                form_validated = False
                new_object = form.instance
            formsets, inline_instances = self._create_formsets(request, new_object, change=not add)
            if all_valid(formsets) and form_validated:

                if "_publish" in request.POST:
                    new_object.published = True
                elif "_unpublish" in request.POST:
                    new_object.published = False

                self.save_model(request, new_object, form, not add)
                self.save_related(request, form, formsets, not add)
                if add:
                    self.log_addition(request, new_object)
                    return self.response_add(request, new_object)
                else:
                    change_message = self.construct_change_message(request, form, formsets)
                    self.log_change(request, new_object, change_message)
                    return self.response_change(request, new_object)
        else:
            if add:
                initial = self.get_changeform_initial_data(request)
                form = ModelForm(initial=initial)
                formsets, inline_instances = self._create_formsets(request, self.model(), change=False)
            else:
                form = ModelForm(instance=obj)
                formsets, inline_instances = self._create_formsets(request, obj, change=True)

        adminForm = helpers.AdminForm(
            form,
            list(self.get_fieldsets(request, obj)),
            self.get_prepopulated_fields(request, obj),
            self.get_readonly_fields(request, obj),
            model_admin=self,
        )
        media = self.media + adminForm.media

        inline_formsets = self.get_inline_formsets(request, formsets, inline_instances, obj)
        for inline_formset in inline_formsets:
            media = media + inline_formset.media

        context = dict(
            self.admin_site.each_context(request),
            title=(_("Add %s") if add else _("Change %s")) % force_text(opts.verbose_name),
            adminform=adminForm,
            object_id=object_id,
            original=obj,
            is_popup=(IS_POPUP_VAR in request.POST or IS_POPUP_VAR in request.GET),
            to_field=to_field,
            media=media,
            inline_admin_formsets=inline_formsets,
            errors=helpers.AdminErrorList(form, formsets),
            preserved_filters=self.get_preserved_filters(request),
        )

        context.update(extra_context or {})

        return self.render_change_form(request, context, add=add, change=not add, obj=obj, form_url=form_url)
开发者ID:cobalys,项目名称:Djournal,代码行数:102,代码来源:admin.py


注:本文中的django.forms.ModelForm.is_valid方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。