Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.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 当我尝试在form.clean_字段方法中访问时,为什么我的form.clean_数据不同?_Django - Fatal编程技术网

Django 当我尝试在form.clean_字段方法中访问时,为什么我的form.clean_数据不同?

Django 当我尝试在form.clean_字段方法中访问时,为什么我的form.clean_数据不同?,django,Django,表单中的数据清理有问题。在我的单个字段清理方法中调用调试器时,清理后的版本中有一半数据丢失。下面是代码的简化版本: class MealForm(forms.Form): def __init__(self, *args, **kwargs): # call super(), do some stuff, then declare some fields: self.fields['meal'] = forms.CharField(required=Tru

表单中的数据清理有问题。在我的单个字段清理方法中调用调试器时,清理后的版本中有一半数据丢失。下面是代码的简化版本:

class MealForm(forms.Form):
    def __init__(self, *args, **kwargs):
        # call super(), do some stuff, then declare some fields:
        self.fields['meal'] = forms.CharField(required=True, max_length=255)
        self.fields['sugar'] = forms.CharField(required=False, max_length=255)
        self.fields['salt'] = forms.CharField(required=False, max_length=255)

    def clean_meal(self):
        pdb.set_trace() # insert a breakpoint to inspect self.cleaned_data.keys() and self.data.keys()
        meal_value = self.cleaned_data['meal']
        # dict of all the various utility functions because python doesn't have 'switch'
        process_meal_type = {
            'dessert': self.process_sugar,
            'main course': self.process_salt,
        }
        try:
            process_meal_type[meal_value]()
        except KeyError:
            raise forms.ValidationError( "Incorrect meal type: " + str(meal_value) )
        return meal_value

   def process_sugar(self):
       if not self.cleaned_data.get('sugar'):
           raise ValidationError ( "Desserts must contain sugar")
       else:
           do_something_with_the_sugar_data()
如果我注释掉
clean\u fine
并在
clean
中放置一个断点,我注意到
self.cleaned\u data
与预期的一样:所有表单字段都存在并占位。我不会在任何地方调用其他字段上的单个
clean.*
方法


发生了什么事?

因为各个字段清理方法提供数据来填充
清理的\u数据
——这就是它们的用途。因此,您无法访问尚未调用清理方法的字段的数据。

因为各个字段清理方法提供数据来填充
清理的\u数据
——这就是它们的用途。因此,您无法访问尚未调用其clean方法的字段的数据。

您如何处理\u fizz/buzz您如何处理?如果您有决心阅读,请阅读文档,以获得编写得非常好的文章thoroughly@Sayse我用实际的名词替换了所有的foobar,使整个事情不那么抽象。让我知道它是否仍然太难理解。你怎么做?如果你有决心阅读的话,请阅读文档以获得写得非常好的文章thoroughly@Sayse我用实际的名词替换了所有的foobar,使整个事情不那么抽象。如果仍然很难理解,请告诉我。我从文档中了解到,
cleaned()
已经被调用:
clean()方法在表单子类上被调用–其中被替换为表单字段属性的名称。。。您需要在self.cleaned_数据中查找字段的值,记住此时它将是一个Python对象,而不是表单中提交的原始字符串(它将在cleaned_数据中,因为上面的general field clean()方法已经清理了数据一次)。
是的,该字段的字段clean已被调用。不适用于任何其他领域。现在我明白了区别。谢谢。我从文档中了解到,
cleaned()
已经被调用:
在表单子类上调用clean()方法–其中被替换为表单字段属性的名称。。。您需要在self.cleaned_数据中查找字段的值,记住此时它将是一个Python对象,而不是表单中提交的原始字符串(它将在cleaned_数据中,因为上面的general field clean()方法已经清理了数据一次)。
是的,该字段的字段clean已被调用。不适用于任何其他领域。现在我明白了区别。谢谢