How to rename a field in a Django form? - django

I have a custom Django widget that renders some HTML that I cannot control, and by default the widget's HTML will has a fixed name attribute using a -, say name="field-name". My problem is that Django expects the name attribute in the HTML to be exactly the same as the variable name of the field in python. But of course in python I cannot name a variable using -.
Is there any way to tell Django to decouple the variable name of the form field from the name in the HTML? In other words, when the user already introduced the data in the field and sends the POST request, can I tell Django that the field field_1 should be reading the value field-name from the requests.POST dictionary?

Related

read only attribute is safe in django forms?

Setting read only attrs for django forms is safe?. In my django projects I do something like this:
def someUpdateView(request):
form = EmployeeForm(instance=Employeem.objects.get(pk=1))
return render...
class EmployeeForm(forms.ModelForm)
declaring here read only attrs field by widgets dict in Meta class or in init method (i.e. pk fields or other fields like email for keeping inmutable).
But what if an user opens web browser inspector mode and edits html field value or deletes read only attribute? during form.save() django will save the new value even it was read only (from html) and if this happens, there is a way to handle that?.

unable to change form field label in django

I'm trying to change the label name of a form field in django, but it won't change. I've changed all instances of the word throughout my code, but still it remains what it originally was. Do I have to delete the form and rewrite it or is there another way to force django to reflect the code?
Use label attribute of your form field.
class MyForm(forms.Form):
myField = forms.CharField(label='My new label')
From Django docs.

How to add auto id to django model form

My django model by default has an auto generated id field as primary key which is named 'id' by default on my db.
As such my model has a lot of other fields that I am selectively displaying using a Django model form. While doing so I specify 'id' for this auto generated id field but my form does not display or generate this field on HTML at all even though it seems to recognize this as a valid field.
I have also checked by doing an 'all' for fields but this is also not bringing in that specific field. The funny thing is django does seem to recognize this field as 'id' because if I change it to 'Id' or anything else random it immediately gives me an error for example "Unknown field(s) (Id) specified for....." on my forms.py
This 'id' is important as I am using it to link and retrieve related objects using Ajax on client side. If this does not work then I will be forced to use another field as primary key. Any suggestions?
I ended up using a workaround as suggested by 'mastazi' for passing this id as a hidden field.
All I did is access this hidden field and generate it as an html element. Since I am using crispy_forms layout I was able to use an HTML element under Layout.
form_id= self.instance.id
id_hidden = '<input type="hidden" id="form_id" value=' + str(form_id) +' />'
I am not sure if this is a perfect solution but it works.

Pagedown(markdown editor) with Django

I trying to use the pagedown(markdown editor), the one that stackoverflow in my django based website. However to get the markdown editor in a textarea it is required to give the text area both id and class as
<textarea id="wmd-input" class="wmd-input"/>
However the form fields generated by django have a default id as id_<field-name>. Is there a way I can assign the same id to this text_area?
you can directly pass id as well in the models.py where your are passing class name. This will override the default behavior.
widgets = {
'<attribute_name>': Textarea(attrs={'class':'wmd-input','id':'wmd-input'}),
}

Django form models | ComboBox value

How do I get the selected value from a form's ComboBox field? what is the model class that deals with ComboBoxes? ..
Thanks.
There's no such thing as a ComboBox in Django (or in HTML). I assume you are talking about a ChoiceField, which renders a select control in HTML.
You access the value of a ChoiceField in exactly the same way as any other field, once the form has been submitted and validated - by accessing form.cleaned_data['fieldname'].
You should read the excellent documentation on forms.
As mentioned by #MMRUser, the ChoiceField is the form class to achieve an HTML select element.
But for the model itself, you can pass the choices argument to a model field (typically a CharField) which will result in the ModelForm using an HTML select element.