Vladimir Lagunov
Vladimir Lagunov

Reputation: 1905

Changing field identifiers without changing field names

Another system makes requests to my server with parameters that look like XYZ_LOGIN=myname&XYZ_PASSWORD=mydata. I can't change that system nor the format.

I parse the data with such a form:

class MyForm(forms.Form):
    XYZ_LOGIN = forms.CharField()
    XYZ_PASSWORD = forms.CharField()

But this is very ugly. Can I make fields with nice aliases like this?

   login = forms.CharField(alias='XYZ_LOGIN')

Upvotes: 1

Views: 195

Answers (1)

Alasdair
Alasdair

Reputation: 308779

You can set the field's label:

XYZ_LOGIN = forms.CharField(label='Login')

As you say in the comments, this only makes the html output "human-friendly" when you render the form, you still have to use cleaned_data.get('XYZ_LOGIN') in your code.

There's no built in functionality similar to the 'alias' argument you propose. You could pre-process the post data before you instantiate the form, or customize the form behaviour. Personally, I would stick with the 'ugliness' of your current code.

Upvotes: 2

Related Questions