Django Data to JsonResponse

ⅰ亾dé卋堺 提交于 2020-01-16 09:06:08

问题


I have this model that store all my data. This is my model.py:

from django.db import models


class Showing(models.Model):
    movie_id = models.CharField(primary_key=True, max_length=11)
    movie_title = models.CharField(max_length=100)
    image_url = models.TextField(blank=True)
    synopsis = models.TextField(blank=True)
    rating = models.TextField(default="MTRCB rating not yet available")
    cast = models.CharField(max_length=100)
    release_date = models.CharField(max_length=50, blank=True)

    def __str__(self):
        return "{}".format(self.movie_id)

And I want to retrieve all my data and pass it to context and make a customize JsonResponse like this:

    "results": [
        {
        "id": "1eb7758d-b950-4198-91b7-1b0ad0d81054",
        "movie": {
        "advisory_rating": "PG",
        "canonical_title": "ROGUE ONE: A STAR WARS STORY",
        "cast": [
        "Felicity Jones",
        "Donnie Yen",
        "Mads Mikkelsen"
        ],
        "poster_portrait": "",
        "release_date": "",
        "synopsis": "Set shortly before the events of Star Wars (1977), Following the foundation of the
        Galactic Empire, the story will center on a wayward band of Rebel fighters which comes together to carry out
        a desperate mission: to steal the plans for the Death Star before it can be used to enforce the Emperor's
        rule. (Source: Disney Pictures)",
        }
        },
       ]

In my views.py I have this function to get all the data, and try just to retrive the movie_id from my model.

def show_all_movies(request):
    movie = Showing.objects.all()
    context = {'result': [{
        'id': movie.movie_id,
        'movie':}]}

But it gets me an error of 'QuerySet' object has no attribute 'movie_id'. So how should i retrieve my data from database so i can parse it to json. thank you.

When I tried to dumps. It returns like this:

"[{\"model\": \"movies.showing\", \"pk\": \"32b05a2f-e52f-55c6-8314-090bdd82e588\", \"fields\": {\"movie_title\": \"0f427f18-23d0-54e3-825e-b56a0f93786f\", \"image_url\": \"http://www.sureseats.com/images/events/movies/thumbnails/geostorm.gif\", \"synopsis\": \"When catastrophic climate change endangers Earth's very survival, world governments unite and create the Dutch Boy Program: a world wide net of satellites, surrounding the planet, that are armed with geoengineering technologies designed to stave off the natural disasters. After successfully protecting the planet for two years, something is starting to go wrong. Two estranged brothers are tasked with solving the program's malfunction before a world wide Geostorm can engulf the planet.(Source: Warner Bros.)\", \"rating\": \"PG\", \"cast\": \"Jeremy Ray Taylor,Gerard Butler,Abbie Cornish\", \"release_date\": \"\"}},


回答1:


in your view movie object is a queryset. you must iterate over it and process any object. for example like this:

def show_all_movies(request):
    movies = Showing.objects.all()

    context={'result':[{
       'id':movie.movie_id,
       'movie': '...'} for movie in movies]}


来源:https://stackoverflow.com/questions/57240043/django-data-to-jsonresponse

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!