0

I have an object that I'm trying to iterate over within my template.

My issue is that one of the fields response contains json data and I get this error message:

transaction' object is not iterable

{% for item in transaction %}
    {{ item.notjson_fields}}
     
    {% for jsonItem in item.response %} 

       {{jsonItem}}

    {% endfor %}

{% endfor %}

Model:

 date_created = models.DateTimeField(auto_now_add=True)
 request = JSONField()
 response = JSONField()
5
  • I assumed this was the error yes. Commented Apr 24, 2013 at 19:11
  • What is transaction? Commented Apr 24, 2013 at 19:15
  • @Ngenator nothing shows. and transaction is a transaction = get_object_or_404(Transaction, vendor_tx_id=id) one field has json data tho in the db Commented Apr 24, 2013 at 19:18
  • values = list(values). whih means Transaction is not an iterable object like list, dict. There should be a method/property(attributet) that you are looking for from transaction. if get_object... is what you are using, it should return an instance of the db record that matches ur query. Commented Apr 24, 2013 at 19:21
  • Is Transaction a ManyToMany relationship? Can you post the definition of Transaction? Commented Apr 24, 2013 at 19:27

2 Answers 2

2

You are trying to iterate over a Transaction object which is not iterable.

Try something like

{{ transaction.notjson_fields}}

{% for jsonItem in transaction.response %} 
    {{ jsonItem }}
{% endfor %}

I can't be sure though without knowing what Transaction looks like


Edit:

Since response is a JSONField, you can access it like a dict. Just do

{{ transaction.response.amount }}
Sign up to request clarification or add additional context in comments.

8 Comments

ok thats seems to work but how do i get the value {{ jsonItem.amount }} is blank where {{ jsonItem }} is all the fields
It may be a string then. Do you deserialize it at all? How is it stored in the database?
response is a direct store of a json request I get ie. in the db it looks like this {"VendorTxCode": "k34m73cr3rhrnghuhsw5drfg7y" etc..}
What is the field type? Are you using something like github.com/bradjasper/django-jsonfield or github.com/derek-schaefer/django-json-field? If not you need to deserialize the data into an object in order to access the key/value pairs.
its a JSONField() in the model
|
1

If like Ngenator said, your field is not a JSON object but a string, you may need to load it first. First register a new template tag

    from django import template
    register = template.Library()
    import json

    @register.filter
    def load_json(value):
        return json.loads(value)

Then to get (just) the amount in your template

    {% with transaction.response|load_json as jsondict %}
    {% for k, v in jsondict.items %}
        {% if k == "amount" %}
            <td>{{ v }}</td>
        {% endif %}   
    {% endfor %}
    {% endwith %}

Good Luck.

4 Comments

you sure is not json as the field in the db is JSONField()?
That may depend on how you pass it from views to template. Try looping the transaction.response in your template and see what you get.
I get a list of fields but not the values
Does {{ jsonItem.VendorTxCode }} have value?

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.