0

I've been encountering this issue for some time today, and a permanent solution would be helpful.

I have a 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 a view, I'm implementing a form to allow for creating new vendors along with updating existing ones. To do this, I had to implement a getVendorData method, which required serializing the data in JSON before sending to the client.

serialized = serializers.serialize("json", Vendors.objects.filter(vendor_id = incomingData["id"]))

In that view, if I use filter in place of get, I don't get a TypeError : Vendors is not iterable message.

To implement create and update functionality in one method, I wrote

def saveVendorData(request):
  incomingData = simplejson.loads(request.POST.get("data", None))

  if incomingData is 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 with data reception in server"}))

When I try to create a new Vendors object with valid data, I get the TypeError response detailed above and generating this 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
for obj in queryset:

TypeError: 'Vendors' object is not iterable

I need to send this data to the client, since creating a new Vendors record results in the insertion of a new HTML element in several locations in the view.

1 Answer 1

3

That error happens because the second param of serialize method is a QuerySet. See here

get returns a Model instande, but filter returns a QuerySet

The arguments to the serialize function are the format to serialize the data to (see Serialization formats) and a QuerySet to serialize.

You could write something like:

serialized = serializers.serialize("json", [vendor])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.