djangoforms FileField validation

224 views
Skip to first unread message

ubervan

unread,
Jul 18, 2008, 4:54:31 AM7/18/08
to Google App Engine
I am getting the following error when using djangoforms.ModelForm:

'NoneType' object has no attribute 'validate'

This is the model:

class Image(BaseModel):
image = db.BlobProperty()
thumb = db.BlobProperty()
owner = db.ReferenceProperty(User)
created = db.DateTimeProperty(auto_now=True)
modified = db.DateTimeProperty(auto_now_add=True)

This is the form:

class ImageForm(djangoforms.ModelForm):
class Meta:
model = Image
exclude = ['thumb', 'ext', 'owner', 'created', 'modified']

This is the code I am using to save the form is just:

imageform = ImageForm(request.POST, request.FILES)

if imageForm.is_valid():
image = imageForm.save(commit=False)
image.thumb = images.resize(image.image, 100, 75)
#etc.......

All this works fine in the development environment but when I upload
to gae I get the error listed above. Does anyone have any ideas why
this wouldn't / doesn't work? Full error listed below:

Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware')


Traceback:
File "/base/data/home/apps/ample/1.55/django/core/handlers/base.py" in
get_response
82. response = callback(request, *callback_args,
**callback_kwargs)
File "/base/data/home/apps/ample/1.55/testmatch/views.py" in
add_multi_choice
252. if form.is_valid() and imageForm.is_valid():
File "/base/data/home/apps/ample/1.55/django/newforms/forms.py" in
is_valid
112. return self.is_bound and not bool(self.errors)
File "/base/data/home/apps/ample/1.55/django/newforms/forms.py" in
_get_errors
103. self.full_clean()
File "/base/data/home/apps/ample/1.55/django/newforms/forms.py" in
full_clean
204. value = field.clean(value, initial)
File "/base/python_lib/versions/1/google/appengine/ext/db/
djangoforms.py" in clean_for_property_field
705. property_clean(prop, value)
File "/base/python_lib/versions/1/google/appengine/ext/db/
djangoforms.py" in property_clean
612. prop.validate(prop.make_value_from_form(value))

Exception Type: AttributeError at /add_choice/
Exception Value: 'NoneType' object has no attribute 'validate'
Message has been deleted

MishaS

unread,
Jul 18, 2008, 6:15:07 AM7/18/08
to Google App Engine
ubervan wrote:
> Installed Middleware:
> ('django.middleware.common.CommonMiddleware',
> 'django.contrib.auth.middleware.AuthenticationMiddleware',
> 'django.contrib.sessions.middleware.SessionMiddleware')
Sorry, it's not really about your original question. But I noticed
this in your mail and wanted to point out to
http://code.google.com/appengine/articles/django.html where they
explicitely say hat auth.middleware and sessions.middleware must be
removed as they require certain django data models that are not
available in Google App Engine.

--
Misha

ubervan

unread,
Jul 21, 2008, 4:42:57 AM7/21/08
to Google App Engine
MichaS Yes thanks I am aware of that, that is why I ma using
django_ae_utils as the session/auth backend.

AndyTI: thanks but it is the actual validation that is not working
i.e. the error id being caused by imageForm.is_valid() being None.
Plus it works fine on dev_appserver just when I upload it it doesn't
work.

Any ideas?

Riverside

unread,
Jul 29, 2008, 8:00:27 AM7/29/08
to Google App Engine
I think it is a bug. When I have upgraded sdk to the newest version, I
have the same problem.
You could try something like this (it works with django 0.97):
class Image(BaseModel):
image = db.BlobProperty()
thumb = db.BlobProperty()
owner = db.ReferenceProperty(User)
created = db.DateTimeProperty(auto_now=True)
modified = db.DateTimeProperty(auto_now_add=True)


class ImageForm(djangoforms.ModelForm):
imagefield = forms.FileField()
class Meta:
model = Image
exclude = ['thumb', 'ext', 'owner', 'created', 'modified',
'image'] #excluding image


if imageForm.is_valid():
imageForm.cleaned_data['image'] =
imageform.cleaned_data['imagefield'].data.read()
image = imageForm.save()

cz

unread,
Aug 6, 2008, 8:42:36 PM8/6/08
to Google App Engine
After a lot of aggravating research and trial and error, this works
for me:

# Model
# FileFields are normally mapped to BlobProperty
class Image(db.Model):
image = db.BlobProperty()

# Form that has a proxy that will
# not be validated during ...is_valid()
class ImageForm(ModelForm):
image_proxy = forms.FileField()
class Meta:
model = Image
exclude = ['image']

# View that handles 'upload image' form
def image_new(request, **kwargs):
if request.method == 'POST':
form = ImageForm(request.POST, request.FILES)
if form.is_valid():
image = form.save(commit=False)
image.image = request.FILES['image_proxy'].read()
image.save()
return HttpResponseRedirect('/wherever')
else:
form = ImageForm(instance=image)
return render_to_response('image_edit.html', [{'form': form}])


# View that handles requests for JPG images
def get_jpg(request, image_id):
image = Image.get_object(id=image_id)
return HttpResponse(image.image, mimetype='image/jpg')


Upload image form template snippet:

<form method="POST" action="" enctype="multipart/form-data">
<table>{{ form }}</table>
<input type="submit" value="upload"/>
</form>


Hopefully I didn't leave anything out - I tried just to show the
relevant code from my more complicated app. I hope this helps. Please
let me know if you still have trouble.
The whole appengine/django marrriage is a little funky. It makes it
hard to back port your project to a straight Django/MySQL environment.

On Jul 29, 5:00 am, Riverside <webpo...@gmail.com> wrote:
> I think it is a bug. When I have upgraded sdk to the newest version, I
...>
Reply all
Reply to author
Forward
0 new messages