set the 'name' attribute of a django form field
I'm struggling for set the 'name' attribute of a form select field with django.
This is what i'm trying :
def make_layers_form(dxf_model):
layers = Layer.objects.filter(dxf_file=dxf_model)
choices = [(m[0], m[1]) for m in settings.MACHINING_CHOICES]
fields = {}
for l in layers:
if l.name.decode() == "0":
# Si le layer 0 existe on l'associe a la "Coupe"
init = settings.MACHINING_CHOICES["CUT"]
else:
# Par défaut on met la valeur "Aucun"
init = settings.MACHINING_CHOICES["NONE"]
fields[l.name] = forms.ChoiceField(choices=choices, initial=init,
widget=fo开发者_高级运维rms.Select(attrs={'name':dxf_model.filename+"_"+str(l.pk)}))
return type('LayersForm', (forms.BaseForm,), {'base_fields':fields})
When I watch to the 'name' attribute, it is not set as expected...
Furthermore I try to set a default value in some case, but it has no effect. Any ideas ?
Thanks for your help!
UPDATE
Here is an html example
<select name="0" id="3_0">
<option value="NONE">Aucun</option>
<option value="CUT">Coupe</option>
<option value="MARK">Marqueur</option>
</select>
...
<select name="0" id="4_0">
<option value="NONE">Aucun</option>
<option value="CUT">Coupe</option>
<option value="MARK">Marqueur</option>
</select>
Each select tag permits to bind some datas to 2 differents files previously uploaded
The value of the attribute 'name' comes from the value of label_tag if I am not wrong.
And the value of the label_tag is a data extracted from each file respectively.
Unfortunately two differents files can contain the same value, which is extracted for setting the attribute 'name'. This is my problem!
So I would define the attribute name of each select tag as it follows: (filename)+"_"+(the value extracted from the file)
I don't know if my explanations are clear...
Do you know why what i'm trying is not working ?
Further more when I set the initial value, it has no effects...
精彩评论