TypeError: Object not iterable - Django create/get -
i've been encountering issue time today, , permanent solution helpful.
i have vendor object defined as
class vendors(models.model): vendor_id = models.integerfield(primary_key=true) vendor_name = models.charfield(max_length=50l) demo_vendor = models.integerfield() url = models.charfield(max_length = 100l, null = true) logo = models.charfield(max_length = 100l, null = true) date_created = models.datetimefield() class meta: db_table = 'txn_vendors'
in view, i'm implementing form allow creating new vendors along updating existing ones. this, had implement getvendordata
method, required serializing data in json before sending client.
serialized = serializers.serialize("json", vendors.objects.filter(vendor_id = incomingdata["id"]))
in view, if use filter
in place of get
, don't typeerror : vendors not iterable
message.
to implement create , update functionality in 1 method, wrote
def savevendordata(request): incomingdata = simplejson.loads(request.post.get("data", none)) if incomingdata not none: vendor = none newvendor = false if incomingdata["id"] == "": vendor = vendors.objects.create(vendor_name = incomingdata["vendor_name"], demo_vendor = false, date_created = datetime.now(), url = incomingdata["vendor_url"], logo = none) newvendor = true else: vendor = vendors.objects.get(vendor_id = incomingdata["id"]) vendor.vendor_name = incomingdata["vendor_name"] vendor.url = incomingdata["vendor_url"] vendor.save() serialized = serializers.serialize("json", vendor) return httpresponse(simplejson.dumps({"success" : "true", "new_vendor" : newvendor, "data" : serialized}), mimetype = "application/json") else: return httpresponse(simplejson.dumps({"success" : "false", "message" : "issue data reception in server"}))
when try create new vendors object valid data, typeerror
response detailed above , generating stacktrace:
vendor data: [{"pk": 5, "model": "app.vendors", "fields": {"url": "nurturing seniors", "demo_vendor": 1, "vendor_name": "nurturing seniors", "date_created": null, "logo": null}}] [15/aug/2013 20:38:01] "post /getvendordata/ http/1.1" 200 218 internal server error: /savevendordata/ traceback (most recent call last): file "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 115, in get_response response = callback(request, *callback_args, **callback_kwargs) file "/media/storage/code_projects/rowdmap_uboat/app/taxonomy_views.py", line 272, in savevendordata serialized = serializers.serialize("json", vendor) file "/usr/local/lib/python2.7/dist-packages/django/core/serializers/__init__.py", line 99, in serialize s.serialize(queryset, **options) file "/usr/local/lib/python2.7/dist-packages/django/core/serializers/base.py", line 42, in serialize obj in queryset: typeerror: 'vendors' object not iterable
i need send data client, since creating new vendors record results in insertion of new html element in several locations in view.
that error happens because second param of serialize
method queryset. see here
get
returns model instande, filter
returns queryset
the arguments serialize function format serialize data (see serialization formats) , queryset serialize.
you write like:
serialized = serializers.serialize("json", [vendor])
Comments
Post a Comment