【问题标题】:Django Data to JsonResponseDjango 数据到 JsonResponse
【发布时间】:2019-12-05 23:30:27
【问题描述】:

我有这个模型来存储我的所有数据。这是我的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)

我想检索我的所有数据并将其传递给上下文并像这样进行自定义 JsonResponse:

    "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)",
        }
        },
       ]

在我的views.py中,我有这个函数来获取所有数据,并尝试从我的模型中检索movie_id。

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

但它给我一个错误'QuerySet'对象没有属性'movie_id'。那么我应该如何从数据库中检索我的数据,以便我可以将其解析为 json。谢谢。

当我试图转储时。它返回如下:

"[{\"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\": \"\"}},

【问题讨论】:

    标签: json django django-models jsonresponse


    【解决方案1】:

    在您看来,电影对象是一个查询集。您必须遍历它并处理任何对象。比如这样:

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

    【讨论】:

    • 非常感谢。现在我可以问我如何将json数据格式化或打印为某种可读格式?就像上面例子中的做法一样?我使用缩进,但它没有做任何事情。谢谢
    • 当您发送 JSON 响应时,所有数据都作为字符串化 JSON 发送。您的客户必须提供数据。 Firefox 随心所欲地显示子结果。
    猜你喜欢
    • 2017-12-24
    • 1970-01-01
    • 2021-03-03
    • 2021-06-12
    • 1970-01-01
    • 1970-01-01
    • 2021-11-26
    • 1970-01-01
    • 2021-03-19
    相关资源
    最近更新 更多