Overriding the HTML id in ModelForm

53 views
Skip to first unread message

Romain

unread,
Mar 4, 2009, 7:05:09 PM3/4/09
to Django users
Hello,

On the same page I have 2 ModelForm that happen to have an attribute
with the same name. Is it possible to choose the name of the HTML id
generated by the form without having to change the real name of the
model attribute?

e.g.
class A(models.Model):
amount = models.IntegerField()

class B(models.Model):
amount = models.IntegerField()


class AForm(ModelForm):
class Meta:
model = A

class BForm(ModelForm):
class Meta:
model = B

Conflict of ids when forms printed:
...
<input type="text" name="amount" id="id_amount" />
...
<input type="text" name="amount" id="id_amount" />
...

Thanks a lot,

Romain

Eric Abrahamsen

unread,
Mar 4, 2009, 9:41:51 PM3/4/09
to django...@googlegroups.com

On Mar 5, 2009, at 8:05 AM, Romain wrote:

>
> Hello,
>
> On the same page I have 2 ModelForm that happen to have an attribute
> with the same name. Is it possible to choose the name of the HTML id
> generated by the form without having to change the real name of the
> model attribute?

A simple solution would be to instantiate your ModelForms with a prefix:
http://docs.djangoproject.com/en/dev/ref/forms/api/#prefixes-for-forms

Another, finer-grained choice is using the auto_id argument:
http://docs.djangoproject.com/en/dev/ref/forms/api/#configuring-html-label-tags

Hope that's what you're looking for,

Eric

Alex Gaynor

unread,
Mar 4, 2009, 10:14:53 PM3/4/09
to django...@googlegroups.com
Auto_I'd won't help. The issue is the names conflict in the POST so
this is an issue for prefix.

Alex

--
"I disapprove of what you say, but I will defend to the death your
right to say it." --Voltaire
"The people's good is the highest law."--Cicero

Eric Abrahamsen

unread,
Mar 5, 2009, 7:42:20 PM3/5/09
to django...@googlegroups.com
Whoops, thanks for pointing that out!

E

Romain

unread,
Mar 16, 2009, 7:45:14 PM3/16/09
to Django users
Thanks a lot for the great answers, and the information about the form
prefix (it is what I was looking for)!

Sorry, I missed the replies 1 week ago :(

Thanks again,

Romain

On Mar 6, 1:42 am, Eric Abrahamsen <gir...@gmail.com> wrote:
> On Mar 5, 2009, at 11:14 AM, Alex Gaynor wrote:
>
>
>
>
>
> > On 3/4/09, Eric Abrahamsen <gir...@gmail.com> wrote:
>
> >> On Mar 5, 2009, at 8:05 AM, Romain wrote:
>
> >>> Hello,
>
> >>> On the same page I have 2 ModelForm that happen to have an attribute
> >>> with the same name. Is it possible to choose the name of theHTMLid
> >>> generated by the form without having to change the real name of the
> >>> model attribute?
>
> >> A simple solution would be to instantiate your ModelForms with a  
> >> prefix:
> >>http://docs.djangoproject.com/en/dev/ref/forms/api/#prefixes-for-
> >> forms
>
> >> Another, finer-grained choice is using the auto_id argument:
> >>http://docs.djangoproject.com/en/dev/ref/forms/api/#configuring-html-...
Reply all
Reply to author
Forward
0 new messages