0

我正在使用 Django 表单向导。在这一部分中,我想使用 if 语句:

def get_form_initial(self, step):
        if step =='2':
                x = self.get_cleaned_data_for_step('0') or {}
                if x['Color']  == 'Red':

无论我尝试什么,我都不会得到 x['Color'] 匹配 'Red'

我检查并 x 获取值:

{'Color': <Products: Red>}

我还尝试了 x['Color']['Products'] == 'Red' (以及许多其他选项),但这导致了一条错误消息。

这可能很容易,但我无法弄清楚......

4

1 回答 1

0

由于您没有指定您正在使用的模型和表单,所以我将来到这里,但我希望它必须是这样的:

def get_form_initial(self, step):
        if step =='2':
                x = self.get_cleaned_data_for_step('0') or {}
                if x['Color'].color  == 'Red':

从我可以收集的内容中x['Color']返回一个对象,我在这里看不到它有什么属性。Products要调试,您可以尝试打印x['Color'].__dict__

于 2013-02-07T21:38:52.640 回答