views:

256

answers:

2

I have a (GoogleAppEngine) Django ModelForm:

class A(db.Model):
  a = db.StringProperty()

class MyAForm(djangoforms.ModelForm):
  class Meta:
    model = A

This creates a form which has one string field a. On my form I'd like to call this something else, say b. So I'd like a form with a field called b and when the form is POSTed we create a new A entity with the a property filled with the b-value.

Is there a neat way to do the plumbing for this?

(p.s. I don't want to change the underlying model)

A: 
class A(db.Model):
    a=db.StringProperty(verbose_name='b')
Till Backhaus
"(p.s. I don't want to change the underlying model)"
Dominic Rodger
sorry, I missed that
Till Backhaus
+3  A: 

It's not clear what you mean when you say you want to 'call' the field something else on your form. Do you mean the label? That's easy to do :

class MyAForm(djangoforms.ModelForm):
  a = forms.CharField(label='b')
  class Meta:
    model = A

If you want to change the underlying HTML ID, you can do that via the widget:

  a = forms.CharField(widget=forms.TextInput(attrs={'id':'b'}))

I don't think it's possible to change the actual field's HTML name, but I can't think of any reason you'd want to do this.

Daniel Roseman
I want to be able to do the following (assuming that I've set the right view):`curl -d"b=my_b_string" http://domain.com/api/A`This creates a new A(a='my_b_string').
nafe
If you have two forms on the same page, where they both contain the name field. This is the scenario I've run into, where I want to alias one of them off, so the request.POST when passed into the form will retrieve the correct variables.
Richard R
@Richard: That's what form prefixes are for: http://docs.djangoproject.com/en/1.1/ref/forms/api/#prefixes-for-forms
Daniel Roseman