ModelMultipleChoiceField

ModelMultipleChoiceField

本文介绍了如何调试ModelMultipleChoiceField的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试记录ModelMultipleChoiceField的值

I try to log value of ModelMultipleChoiceField

logger.debug("names %s" % self.fields['names_list'])

但是我有

  2016-08-09 17:00:19,027 DEBUG views names <django.forms.models.ModelMultipleChoiceField object at 0xa74d0bcc>

预期结果如下:

  2016-08-09 17:00:19,027 DEBUG views names [33,40,45]

更新
我不知道什么是不清楚的 - 我想,你想看到的模型,但我认为是无用的 - 我不我需要调试,因为当我输入表单时,我设置了一些隐藏的字段:

updateI'm not sure what is unclear - I suppose, that you want to see the model, but i think is useless - i don't need any value just pk of instances which are selected in ModelMultipleChoiceField

b

<input id="id_names" name="names" type="hidden" value="[33]" />

我正在尝试使用附加字段(使用ModelMultipleChoiceField)设置它 - 在清理期间正确的检查和设置值。但是看起来像ModelMultipleChoiceField是干净的,所以我想调试,如果它是真的。

and I'm trying to set it using additional field (using ModelMultipleChoiceField) - during clean I'm veryfing checkings and set values. But it looks like ModelMultipleChoiceField is empty in clean, so I want to debug if it's true.

(我必须将字段名称重命名为names_list,因为您可能认为隐藏字段和multiplechoicefield具有相同的名称)

(I have to rename field names into names_list because you may think that hidden field and multiplechoicefield have the same names)

推荐答案

您可以将 ModelMultipleChoiceField 转换为

selected_names = [label for value, label in self.fields['names'].choices if value in self['names'].value()]
logger.debug("names %s" % selected_names)

这篇关于如何调试ModelMultipleChoiceField的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 13:50