12

I need to convert JSON data to django model.

This is my JSON data

{
  "data": [
    {
      "id": "20ad5d9c-b32e-4599-8866-a3aaa5ac77de",
      "name": "name_1"
    },
    {
      "id": "7b6d76cc-86cd-40f8-be90-af6ced7fec44",
      "name": "name_2"
    },
    {
      "id": "b8843b1a-9eb0-499f-ba64-25e436f04c4b",
      "name": "name_3"
    }
  ]
}

This is my django method

def get_titles():
    url = 'http://localhost:8080/titles/' 
    r = requests.get(url)
    titles = r.json()
    print(titles['data'])

What I need is convert the JSON into an instance of the model and pass it to the template. Please let me know how to convert JSON to Model.

1 Answer 1

15

Using JSON in Django templates

You don't have to convert the JSON structure into a Django model just to use it in a Django template: JSON structures (Python dicts) work just fine in a Django template

e.g. if you pass in {'titles': titles['data']} as the context to your template, you can use it as:

{% for title in titles %}
    ID is {{title.id}}, and name is {{title.name}}
{% endfor %}

As long as you don't need to store the data with Django, the above solution works just fine. If you want to store, read below.

Make a model

You can create a model to store that JSON data in. Once stored you can pass the queryset to your template

class Title(models.Model)
    id = models.CharField(max_length=36)
    name = models.CharField(max_length=255)

or use an UUIDField

class Title(models.Model)
    id = models.UUIDField(primary_key=True)
    name = models.CharField(max_length=255)

Store the data in a Django model

# Read the JSON
titles = r.json()
# Create a Django model object for each object in the JSON 
for title in titles['data']:
    Title.objects.create(id=title['id'], name=title['name'])

Use stored data to pass as template context

# Then pass this dict below as the template context
context = {'titles': Title.objects.all()}
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks you'r comment. I've an question. I didn't config the database. It just consume REST service. How do I create object and get all objects that context?
Put the Title model I described in my answer, in a file inside your Django app called models.py, add your app to INSTALLED_APPS in your settings.py, run database migration to create the SQL tables etc, more on that here: docs.djangoproject.com/en/1.9/topics/db/models
If want a model without a DB table, then you don't really need a model in the Django sense :) You are fine with the JSON/Python dict in the first paragraph of the answer. Try create a view, and a template e.g. titles.html and just pass in the JSON data you have. Post another question if you get stuck I'll help if I see it!
If you're thinking about "Model" in MVC pattern, in Django it's not required to be a Django ORM Model, it can be a Python object, Python dict (e.g. JSON). You don't need a Django model to pass data to the template.
This does not answer the question asked, either edit the question's title or edit the answer... It is misleading at best.
|

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.