how to debug ModelMultipleChoiceField [closed]

2024/10/14 15:24:21

I try to log value of ModelMultipleChoiceField

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

But I got

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

The expected result is sth like:

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

update I'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

I need to debug because when I enter form i set some hidden fields:

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

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.

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

Answer

You can convert the ModelMultipleChoiceField to a list of strings with

selected_names = [label for value, label in self.fields['names'].choices if value in self['names'].value()]
logger.debug("names %s" % selected_names)
https://en.xdnf.cn/q/117944.html

Related Q&A

Standardization/preprocessing for 4-dimensional array

Id like to standardize my data to zero mean and std = 1. The shape of my data is 28783x4x24x7, and it can thought of as 28783 images with 4 channels and dimensions 24x7. The channels need to be standar…

My Python number guessing game

I have been trying to make a number guessing game for Python and so far it has altogether gone quite well. But what keeps bugging me is that it resets the number on every guess so that it is different,…

Class that takes another class as argument, copies behavior

Id like to create a class in Python that takes a single argument in the constructor, another Python class. The instance of the Copy class should have all the attributes and methods of the original clas…

Simple python script to get a libreoffice base field and play on vlc

Ive banged my head for hours on this one, and I dont understand the LibreOffice macro api well enough to know how to make this work:1) This script works in python:#!/usr/bin/env python3 import subproce…

Print month using the month and day

I need to print month using the month and day. But I cannot seem to move the numbers after 1 to the next line using Python.# This program shows example of "November" as month and "Sunday…

Maya: Defer a script until after VRay is registered?

Im trying to delay a part of my pipeline tool (which runs during the startup of Maya) to run after VRay has been registered. Im currently delaying the initialization of the tool in a userSetup.py like…

Optimization on Python list comprehension

[getattr(x, contact_field_map[communication_type])for x in curr_role_group.contacts ifgetattr(x, contact_field_map[communication_type])]The above is my list comprehension. The initial function and the …

tensorflow Word2Vec error

I downloaded source code of word2vec in github below. https://github.com/tensorflow/models/blob/master/tutorials/embedding/word2vec.py I am using tensorflow on pycharm. Im using windows 10. I installed…

Mysterious characters at the end of E-Mail, received with socket in python

I am not sure if this is the right forum to ask, but I give it a try. A device is sending an E-Mail to my code in which I am trying to receive the email via a socket in python, and to decode the E-Mail…

Error when importingPython-vlc

I have installed python-vlc D:\Programing\Python\Python 3.6>python -m pip install python-vlc Requirement already satisfied: python-vlc in d:\programing\python\python 3.6\lib\site-packages\python_vlc…