django forms - setting input control attributes

586 views
Skip to first unread message

koval

unread,
May 1, 2008, 2:58:44 PM5/1/08
to Google App Engine
For example - with a simple db.Model like:

class Category(db.Model):
name = db.StringProperty(required=True)

... and a follownig djangoforms.ModelForm instance:

class CategoryForm(djangoforms.ModelForm):
class Meta:
model = Category

What is the preferred way of setting html's input attributes like
size,maxlength,class ?

natmartin

unread,
May 2, 2008, 4:51:23 PM5/2/08
to Google App Engine
I'm interested in this as well. I'm also curious, if you're using a
model form, how do you add a field that's not in the model?

-Nate

randomm

unread,
May 14, 2008, 4:57:56 AM5/14/08
to Google App Engine
Ditto... the source code ModelForm states: "It is also possible to
define form fields explicitly. This gives more control over the
widget used, constraints, initial value, and so on. Such form fields
are not affected by the nested Meta class's fields and exclude
attributes." ... but how that is done still eludes me...

anyone??

J

On May 2, 11:51 pm, natmartin <natmar...@gmail.com> wrote:
> I'm interested in this as well. I'm also curious, if you're using a
> model form, how do you add a field that's not in the model?
>
> -Nate
>
> On May 1, 11:58 am, koval <ko...@konto.pl> wrote:
>
> > For example - with a simple db.Model  like:
>
> > class Category(db.Model):
> >     name = db.StringProperty(required=True)
>
> > ... and a follownigdjangoforms.ModelForm instance:

Matt Kangas

unread,
May 15, 2008, 10:26:21 AM5/15/08
to Google App Engine
I was also very curious about this. Finally figured it out a few days
ago -- you simply override the default form element definitions that
ModelForm creates. Example:

class ListingForm(djangoforms.ModelForm):
# custom form fields
category = forms.CharField(widget=forms.HiddenInput())
title =
forms.CharField(widget=forms.TextInput(attrs={'size':'60','maxlength':'70'}))
price =
forms.CharField(widget=forms.TextInput(attrs={'size':'10','maxlength':'10'}))
description =
forms.CharField(widget=forms.Textarea(attrs={'rows':'10','cols':'70'}))

class Meta:
model = datamodel.Listing
exclude = ['is_archived','seller','buyer']

Matt Kangas

unread,
May 15, 2008, 10:28:07 AM5/15/08
to Google App Engine
Doh, lines wrapped on my post. "title = forms.CharField(..)" should be
one line, natch

Jeff Hinrichs

unread,
May 15, 2008, 11:57:30 AM5/15/08
to Google App Engine
Thank you! That was just what I needed to figure out how to sort a
dropdown for a ReferenceProperty. I've been trying to figure out how
to show a ReferenceProperty dropdown in a humane order.

Here is an example if anyone else is interested:

class SchoolForm(djangoforms.ModelForm):
'''school form based on entity'''
State = djangoforms.ModelChoiceField(States,
query=db.Query(States).order('StateName'))
class Meta:
model = Schools
exclude = []

randomm

unread,
May 16, 2008, 6:33:56 AM5/16/08
to Google App Engine
brilliant stuff! thanks very much...

randomm

unread,
May 19, 2008, 9:24:30 AM5/19/08
to Google App Engine
Ah, but are you using django's own newforms library, or GAE's
"djangoforms" library? ... this does not seem to work in my app when
I'm using djangoforms ala GAE. I can only use the ModelChoiceField,
not CharField or FloatField or ...

if I try I get:

<type 'exceptions.AttributeError'>: 'module' object has no attribute
'FloatField'

cheers!

J



On May 15, 5:26 pm, Matt Kangas <m...@ansonia01.com> wrote:

randomm

unread,
May 22, 2008, 8:23:26 AM5/22/08
to Google App Engine
Soo... nobody has any idea how to use these django specific fields
with GAE??

On May 19, 4:24 pm, randomm <turunen.j...@gmail.com> wrote:
> Ah, but are you using django's own newforms library, or GAE's
> "djangoforms" library? ... this does not seem to work in my app when
> I'm using djangoforms ala GAE. I can only use the ModelChoiceField,
> notCharFieldor FloatField or ...

SeanOC

unread,
May 22, 2008, 1:14:54 PM5/22/08
to Google App Engine
> > Ah, but are you using django's own newforms library, or GAE's
> > "djangoforms" library?

You'd use the newforms library to define the form fields and the GAE
djangoforms to define the form itself.

This is similar to a common point of confusion that many people have
with just straight Django. In Django there is the NewForms library,
Models, and ModelForms. The NewForms library is only responsible for
rendering and validating HTML forms. Models are only responsible for
storing an retrieving data from the database. NewForms, in itself has
nothing to do with models and visa versa. ModelForms then simply ties
the two parts together. A ModelForm is a form which knows how to load
data from and save data to the model. Accordingly when you wish to
override a field in the form you simply do so with the NewForms fields
(i.e. forms.CharField).

In GAE this all still holds true, you are simply swapping out the
Django models for GAE datastore models and ModelForms for
google.appengine.ext.db.djangoforms.ModelForm. This means that the
Forms part of the ModelForm hasn't changed so you can still override
fields using NewForms fields

theaellen

unread,
May 24, 2008, 10:30:24 AM5/24/08
to Google App Engine
Kangas' form example (above) worked for me after I added this line to
the top of the page:

from django import newforms as forms

(I put it after the other from django stuff) Now all the includes on
my views.py file looks like this:

import os

from google.appengine.api import users
from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

from google.appengine.ext.db import djangoforms

import django
from django import http
from django import shortcuts
from django import newforms as forms

Another thing needed is the django_bootstrap.py file that I got off
the Google Code forum. If you're new and looking for a place to
start, try with example that makes a gift list, using Django forms
with the App Engine. So far, it is the best working example I have
found that uses the Django forms correctly.

http://code.google.com/p/google-app-engine-samples/downloads/list

from the list pick the file django_example_20080409.tar.gz

randomm

unread,
May 26, 2008, 11:04:48 AM5/26/08
to Google App Engine
Thanks for this.

Am I right in assuming that to be able to use newforms from v0.97 one
has to somehow bundle django together with one's application - as the
stuff that comes with GAE SDK seems to be 0.96 (no FileField etc...
and newforms merely linking to oldforms in the bundled django)?
Reply all
Reply to author
Forward
Message has been deleted
0 new messages