English 中文(简体)
django 选择植被更新,一度通过选择。
原标题:django select widget doesnt update when i pass the choices into it

在我看来,这样做是为了作为形式的一部分更新某些植被。

i e seen tons of stuff on how to do it, i ve followed it and got nearly there. i ve got a bit of code below which is called to populate the select with the choices and it does, but i think the formatting is out, as its passing back a unicode string and i think it needs to be a tuple.

assigning the choices

form.fields[ size_option ].widget.attrs[ choices ] = Product.get_options(product)

产生选择的法典

def get_options(self):
    optionset = "("
    for option in self.optionset.options.all():
        optionset = optionset + "( " + option.name + " ,  " + option.name + " )"
    optionset = optionset + ")"
    pdb.set_trace()
    return optionset

选择的html如下。

<select id="id_size_option" name="size_option" choices="((&#39;Small&#39;, &#39;Small&#39;)(&#39;Medium&#39;, &#39;Medium&#39;)(&#39;Large&#39;, &#39;Large&#39;))"> 

因此,问题可能是背后的各种选择。 i 可以想象。 i 根本不知道什么错误。 i 无法找到文件,说明如何在选择中加以编排。

最佳回答

应该做些什么? 选择清单的格式是标准指南:

CHOICES = (
    ( x ,  choice x ),
    ( y ,  choice y ),
)

因此,我不理解你试图与所有这些扼制格式做什么。

第二,<代码>choices不是植被图(attrs/code>的一个要素,而是实地和(或)植被本身的特性:

form.fields[ size_option ].choices = product.get_options()

无论如何,你可能希望在这里使用示范Choice油田,然后你可以确定<代码>queryset,归属于你想要的备选办法清单。

最后,你没有用<代码>Class.method(instance)的示例方法,请上instance.method()——就你而言,product.get_options (

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签