使用 Django 处理 JSON 响应
Handling JSON Response With Django
Django 1.9 Python3.5
我正在使用 tastypie 来消费外部 api
我正在创建一个 class 来处理来自 restful api 的 json 响应。我知道从 api 返回的数据类型,但不知道如何声明其中的一些。
这是我在 Golang 中声明它们的方式
PosterPath string `json:"poster_path"`
Adult bool `json:"adult"`
Overview string `json:"overview"`
ReleaseDate string `json:"release_date"`
GenreIds []int `json:"genre_ids"`
Id int `json:"id"`
OriginalTitle string `json:"original_title"`
OriginalLanguage string `json:"original_language"`
Title string `json:"title"`
BackdropPath string `json:"backdrop_path"`
Popularity float64 `json:"popularity"`
VoteCount int `json:"vote_count"`
Video bool `json:"video"`
VoteAverage float64 `json:"vote_average"`
根据我的理解,这就是我应该如何使用 DJjango 声明它们
class Movies(Resource):
PosterPath = fields.URLField(attribute='poster_path')
Adult = fields.BooleanField(attribute='adult')
Overview = fields.CharField(attribute='overview')
ReleaseDate = fields.CharField(attribute='release_date')
GenreIds = fields.**Array of Ints**(attribute='genre_ids')
Id = fields.IntegerField(attribute='id')
OriginalTitle = fields.CharField(attribute='original_title')
OriginalLanguage = fields.CharField(attribute='original_language')
Title = fields.CharField(attribute='title')
BackdropPath = fields.URLField(attribute='backdrop_path')
Popularity = fields.DecimalField(attribute='popularity')
VoteCount = fields.IntegerField(attribute='vote_count')
Video = fields.BooleanField(attribute='video')
VoteAverage = fields.DecimalField(attribute='vote_average')
这个字段
GenreIds = fields.**Array of Ints**(attribute='genre_ids')
它是一个整数数组。处理这种类型的 json 的正确方法是什么?看起来像这样,
"genre_ids": [
18,
10402
],
django 中有 Postgres 特定的 ArrayField。您也可以将接收到的数组转换为字符串并将其存储在与数据库无关的 CommaSeparatedIntegerField 中。
PS:我相信你使用模型的方式不对。应通过“models”模块访问字段 类,而不是“fileds”模块(即
models.BooleanField 而不是
fields.BooleanField)
Django 1.9 Python3.5
我正在使用 tastypie 来消费外部 api
我正在创建一个 class 来处理来自 restful api 的 json 响应。我知道从 api 返回的数据类型,但不知道如何声明其中的一些。
这是我在 Golang 中声明它们的方式
PosterPath string `json:"poster_path"`
Adult bool `json:"adult"`
Overview string `json:"overview"`
ReleaseDate string `json:"release_date"`
GenreIds []int `json:"genre_ids"`
Id int `json:"id"`
OriginalTitle string `json:"original_title"`
OriginalLanguage string `json:"original_language"`
Title string `json:"title"`
BackdropPath string `json:"backdrop_path"`
Popularity float64 `json:"popularity"`
VoteCount int `json:"vote_count"`
Video bool `json:"video"`
VoteAverage float64 `json:"vote_average"`
根据我的理解,这就是我应该如何使用 DJjango 声明它们
class Movies(Resource):
PosterPath = fields.URLField(attribute='poster_path')
Adult = fields.BooleanField(attribute='adult')
Overview = fields.CharField(attribute='overview')
ReleaseDate = fields.CharField(attribute='release_date')
GenreIds = fields.**Array of Ints**(attribute='genre_ids')
Id = fields.IntegerField(attribute='id')
OriginalTitle = fields.CharField(attribute='original_title')
OriginalLanguage = fields.CharField(attribute='original_language')
Title = fields.CharField(attribute='title')
BackdropPath = fields.URLField(attribute='backdrop_path')
Popularity = fields.DecimalField(attribute='popularity')
VoteCount = fields.IntegerField(attribute='vote_count')
Video = fields.BooleanField(attribute='video')
VoteAverage = fields.DecimalField(attribute='vote_average')
这个字段
GenreIds = fields.**Array of Ints**(attribute='genre_ids')
它是一个整数数组。处理这种类型的 json 的正确方法是什么?看起来像这样,
"genre_ids": [
18,
10402
],
django 中有 Postgres 特定的 ArrayField。您也可以将接收到的数组转换为字符串并将其存储在与数据库无关的 CommaSeparatedIntegerField 中。
PS:我相信你使用模型的方式不对。应通过“models”模块访问字段 类,而不是“fileds”模块(即 models.BooleanField 而不是 fields.BooleanField)