Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 为什么我的表单清理方法没有做任何事情?_Django_Django Forms_Django Admin - Fatal编程技术网

Django 为什么我的表单清理方法没有做任何事情?

Django 为什么我的表单清理方法没有做任何事情?,django,django-forms,django-admin,Django,Django Forms,Django Admin,我有两个基本模型,它们在Django管理中使用模型表单。 Models.py类似于: class FirstModel(models.Model): name = CharField(max_length=100) url = URLField() class OtherModel(models.Model): model = models.ForeignKey(FirstModel) ##Other fields that show up fine and sa

我有两个基本模型,它们在Django管理中使用模型表单。 Models.py类似于:

class FirstModel(models.Model):
    name = CharField(max_length=100)
    url = URLField()
class OtherModel(models.Model):
    model = models.ForeignKey(FirstModel)
    ##Other fields that show up fine and save fine, but include some localflavor
class FirstModelForm(forms.ModelForm):
    def clean(self):
        #call the super as per django docs
        cleaned_data = super(FirstModelForm, self).clean()
        print cleaned_data
    class Meta:
        model = FirstModel
#other modelform is the same with the appropriate word substitutions and one field that gets overridden to a USZipCodeField
Forms.py看起来类似于:

class FirstModel(models.Model):
    name = CharField(max_length=100)
    url = URLField()
class OtherModel(models.Model):
    model = models.ForeignKey(FirstModel)
    ##Other fields that show up fine and save fine, but include some localflavor
class FirstModelForm(forms.ModelForm):
    def clean(self):
        #call the super as per django docs
        cleaned_data = super(FirstModelForm, self).clean()
        print cleaned_data
    class Meta:
        model = FirstModel
#other modelform is the same with the appropriate word substitutions and one field that gets overridden to a USZipCodeField
这是一个堆叠的内联模型admin,admin.py中没有任何特殊内容:

class OtherModelInline(admin.StackedInline):

    model = OtherModel
    fields = (#my list of fields works correctly)
    readonly_fields = (#couple read onlys that work correctly)

class FirstModelAdmin(admin.ModelAdmin):
    inlines = [
        OtherModelInline,
        ]
admin.site.register(FirstModel, FirstModelAdmin)
我有一个用户模型form and ModelAdmin,它将User和UserCreationForm子类化,并覆盖它自己的clean方法。这完全符合预期。 问题在于
FirstModel
OtherModel
。我在
FirstModelForm
OtherModelForm
的ModelForm子类中重写的干净方法没有任何作用。未引发异常或打印已清理的\u数据。没什么。其他一切都按预期工作,但好像我的clean方法根本不存在。
我错过了一些简单的东西,但我看不出是什么。任何帮助都会很好。谢谢

您需要从
clean
方法返回表单中的
cleaned_数据。如果你看一下,你会注意到:

...
# Always return the full collection of cleaned data.
return cleaned_data

您需要从表单中的
clean
方法返回
cleaned_数据。如果你看一下,你会注意到:

...
# Always return the full collection of cleaned data.
return cleaned_data

父“clean”方法可能无法保存任何内容。如果您提交的数据由于模型的设置方式而无法验证,则清除的数据将为空。Timmy链接的同一份文件中提到了这一点,其中写道:

在调用表单的clean()方法时,所有单独的字段clean方法都已运行(前两部分),因此self.cleaned_数据将填充迄今为止保留的所有数据因此,您还需要记住,要验证的字段可能在最初的单个字段检查中无法存活。

在这种情况下,如果您有一个URL字段,那么字段验证是非常严格的,除非您定义“verify_exists=False”,否则它还会检查您是否输入了返回404的URL。在您的情况下,如果您希望允许,您需要这样做:

class FirstModel(models.Model):
    name = CharField(max_length=100)
    url = URLField(verify_exists=False)

除此之外,我不知道会发生什么。

有可能没有什么东西在父“干净”方法中幸存下来。如果您提交的数据由于模型的设置方式而无法验证,则清除的数据将为空。Timmy链接的同一份文件中提到了这一点,其中写道:

在调用表单的clean()方法时,所有单独的字段clean方法都已运行(前两部分),因此self.cleaned_数据将填充迄今为止保留的所有数据因此,您还需要记住,要验证的字段可能在最初的单个字段检查中无法存活。

在这种情况下,如果您有一个URL字段,那么字段验证是非常严格的,除非您定义“verify_exists=False”,否则它还会检查您是否输入了返回404的URL。在您的情况下,如果您希望允许,您需要这样做:

class FirstModel(models.Model):
    name = CharField(max_length=100)
    url = URLField(verify_exists=False)

除此之外,我不知道会发生什么。

默认情况下,Django会为模型管理员动态生成一个模型表单。必须通过设置表单属性指定要使用自定义表单

class OtherModelInline(admin.StackedInline):

    model = OtherModel
    fields = (...) # if this doesn't work after specifying the form, set fields for the model form instead
    readonly_fields = (#couple read onlys that work correctly)
    form = OtherModelForm

class FirstModelAdmin(admin.ModelAdmin):
    form = FirstModelForm
    inlines = [
        OtherModelInline,
        ]
admin.site.register(FirstModel, FirstModelAdmin)

默认情况下,Django为模型管理员动态生成模型表单。必须通过设置表单属性指定要使用自定义表单

class OtherModelInline(admin.StackedInline):

    model = OtherModel
    fields = (...) # if this doesn't work after specifying the form, set fields for the model form instead
    readonly_fields = (#couple read onlys that work correctly)
    form = OtherModelForm

class FirstModelAdmin(admin.ModelAdmin):
    form = FirstModelForm
    inlines = [
        OtherModelInline,
        ]
admin.site.register(FirstModel, FirstModelAdmin)

我懂了。谢谢,但这不会导致我的clean方法不起任何作用。我的
已清理的\u数据
的控制台上仍应有打印输出。我明白了。谢谢,但这不会导致我的clean方法不起任何作用。我的
已清理的\u数据
的控制台上还应该有打印输出。谢谢!这正是我应该知道的,因为我在UserModelAdmin中分配了表单。我知道这很简单,我只是有时会遭受严重的功能性固定。再次感谢,谢谢!这正是我应该知道的,因为我在UserModelAdmin中分配了表单。我知道这很简单,我只是有时会遭受严重的功能性固定。再次感谢。谢谢你的回答,但不是我想要的。虽然我不知道
verify_exists
参数,但我现在知道了。没问题,很高兴有人帮您排序。我已经好几年没有使用内置的管理员了:)谢谢你的回答,但不是我想要的。虽然我不知道
verify_exists
参数,但我现在知道了。没问题,很高兴有人帮您排序。我已经好几年没有使用内置的管理员了:)