如何过滤Django数据库中的记录?

时间:2019-07-29 01:02:44

标签: django django-orm django-filter django-database

我有这些具有相同movie_id和Cinema_name的数据,但我希望它们作为一个列出。我正在为数据库使用sqlite3。这是我的示例数据:

    {
    "result": [
        {
            "id": "06273d05-dd3a-5738-bf44-3eca7e329969",
            "schedule": {
                "cinema": "5",
                "movie": "0f427f18-23d0-54e3-825e-b56a0f93786f",
                "price": "500.00",
                "seating_type": "Guaranteed Seats",
                "show_date": "18 Oct 2017",
                "start_time": "02 45 00 PM",
                "theater": "GB3",
                "variants": "3D/4DX"
            }
        }
    ]
},
{
    "result": [
        {
            "id": "8954b8b4-9ffb-5d19-a4eb-6cc296bc6aaa",
            "schedule": {
                "cinema": "5",
                "movie": "0f427f18-23d0-54e3-825e-b56a0f93786f",
                "price": "500.00",
                "seating_type": "Guaranteed Seats",
                "show_date": "18 Oct 2017",
                "start_time": "05 10 00 PM",
                "theater": "GB3",
                "variants": "3D/4DX"
            }
        }
    ]
},
    {
    "result": [
        {
            "id": "1a7ed3d2-c05f-57e6-84aa-24870d47d43e",
            "schedule": {
                "cinema": "4",
                "movie": "0f427f18-23d0-54e3-825e-b56a0f93786f",
                "price": "500.00",
                "seating_type": "Guaranteed Seats",
                "show_date": "18 Oct 2017",
                "start_time": "12 20 00 PM",
                "theater": "BHS",
                "variants": "3D/4DX"
            }
        }
    ]
},
{
    "result": [
        {
            "id": "bb749fd7-b5f6-5fda-84a2-149fc3053b95",
            "schedule": {
                "cinema": "4",
                "movie": "0f427f18-23d0-54e3-825e-b56a0f93786f",
                "price": "500.00",
                "seating_type": "Guaranteed Seats",
                "show_date": "18 Oct 2017",
                "start_time": "02 45 00 PM"
                "theater": "BHS",
                "variants": "3D/4DX"
            }
        }
    ]
}

如何按剧院或电影院对它们进行分组,这样我将获得如下数据:

    "result": [
        {
            "id": "bb749fd7-b5f6-5fda-84a2-149fc3053b95",
            "schedule": {
                "cinema": "4",
                "movie": "0f427f18-23d0-54e3-825e-b56a0f93786f",
                "price": "500.00",
                "seating_type": "Guaranteed Seats",
                "show_date": "18 Oct 2017",
                "start_time": [ "12 20 00 PM","02 45 00 PM"]
                "theater": "BHS",
                "variants": "3D/4DX"
            }
        }
    ]
}

请注意,每个数据的时间成为要在一个数据中显示的列表。

我已经对我的views.py尝试过

def showing(request, movie_id):

shows = Schedules.objects.filter(movie_id=movie_id)
showings = list()

for show in shows:
    str_date = show.screening.date()
    date=str_date.strftime("%d %b %Y")
    str_time = show.screening.time()
    time=str_time.strftime("%I %M %S %p")

    results = {'result':[
    {'id' : show.schedule_id,
     'schedule':{
     'cinema':show.cinema_name,
     'movie': show.movie_title,
     'price': show.price,
     'seating_type': show.seat_type,
     'show_date': date,
     'start_time':time,
     'theater': show.theater_code,
     'variants': show.variant
     }
    }
    ]}

    showings.append(results)
    return HttpResponse(json.dumps(showings, indent=4), 
    content_type="application/json")

0 个答案:

没有答案