1. Jesper Noehr
  2. django-piston
  3. Issues
Issue #113 open

POST fails if there are empty fields

Vincenzo Ampolo
created an issue

{{{

!bash

goshawk@earth:~/Downloads$ sudo easy_install django-piston-c0de361e19e5.gz Processing django-piston-c0de361e19e5.gz Running django-piston/setup.py -q bdist_egg --dist-dir /tmp/easy_install-FXtOZk/django-piston/egg-dist-tmp-gUMtRs Removing django-piston 0.2.2 from easy-install.pth file Adding django-piston 0.2.3rc1 to easy-install.pth file

Installed /usr/local/lib/python2.6/dist-packages/django_piston-0.2.3rc1-py2.6.egg Processing dependencies for django-piston==0.2.3rc1 Finished processing dependencies for django-piston==0.2.3rc1 }}}

So i've installed trunk version

If i submit a post with curl with empty fields i get:

{{{

!bash

goshawk@earth:~$ curl -X POST http://localhost:8000/api/Persona.json -d "nome=testjson1&cognome=testjson&secondo_cognome=&indirizzo=&numero_civico=&citta=&provincia=&cap=&telefono=&cellulare=&email=&perdita_media=&note=&preventivo="

Piston/0.2.3rc1 (Django 1.1.1) crash report:

Traceback (most recent call last):

File "/usr/local/lib/python2.6/dist-packages/django_piston-0.2.3rc1-py2.6.egg/piston/handler.py", line 103, in create inst = self.queryset(request).get(**attrs)

File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 299, in get clone = self.filter(args, *kwargs)

File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 498, in filter return self._filter_or_exclude(False, args, *kwargs)

File "/usr/lib/pymodules/python2.6/django/db/models/query.py", line 516, in _filter_or_exclude clone.query.add_q(Q(args, *kwargs))

File "/usr/lib/pymodules/python2.6/django/db/models/sql/query.py", line 1675, in add_q can_reuse=used_aliases)

File "/usr/lib/pymodules/python2.6/django/db/models/sql/query.py", line 1614, in add_filter connector)

File "/usr/lib/pymodules/python2.6/django/db/models/sql/where.py", line 56, in add obj, params = obj.process(lookup_type, value)

File "/usr/lib/pymodules/python2.6/django/db/models/sql/where.py", line 269, in process params = self.field.get_db_prep_lookup(lookup_type, value)

File "/usr/lib/pymodules/python2.6/django/db/models/fields/init.py", line 210, in get_db_prep_lookup return [self.get_db_prep_value(value)]

File "/usr/lib/pymodules/python2.6/django/db/models/fields/init.py", line 698, in get_db_prep_value return int(value)

ValueError: invalid literal for int() with base 10: ''

goshawk@earth:~$ }}}

Comments (7)

  1. Vincenzo Ampolo reporter

    Of course if i delete all the empty fields it works

    goshawk@earth:~$ curl -X  POST http://localhost:8000/api/Persona.json -d "nome=testjson2&cognome=testjson"
    {
        "spontaneo": false, 
        "venuto": false, 
        "indirizzo": "", 
        "provincia": null, 
        "perdita_media": null, 
        "nome": "testjson2", 
        "cognome": "testjson", 
        "cap": null, 
        "telefono": null, 
        "note": null, 
        "problemi_udito": false, 
        "secondo_cognome": null, 
        "preventivo": null, 
        "cellulare": null, 
        "numero_civico": null, 
        "cliente": false, 
        "citta": "", 
        "email": null, 
        "porta_apparecchio": false
    }
    
  2. Vincenzo Ampolo reporter

    Just change in piston/handlers.py this part of the code

    line 55

    def flatten_dict(self, dct):
            return dict([ (str(k), dct.get(k)) for k in dct.keys() ])
    

    with

    def flatten_dict(self, dct):
            return dict([(str(k), dct.get(k)) for k in dct.keys() if dct.get(k) != ''])
    
  3. Log in to comment