0

Django 1.9 Python3.5

I am using tastypie to consume an external api

I am creating a class to handle the json response from a restful api. I know the data types coming back from the api but do not know how to declare a few of them.

This is how I declare them in 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"` 

From my understanding this is how I should declare them with 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')

This field

GenreIds = fields.**Array of Ints**(attribute='genre_ids') 

It is an array of ints. What is the correct way to handle the json of this type? It looks like this,

"genre_ids": [
        18,
        10402
      ],

1 Answer 1

1

There is Postgres specific ArrayField in django. Also you may transform resceived array to string and store it in CommaSeparatedIntegerField which is db agnostic.

PS: I believe you are using models in wrong way. Field classes should be accessed via 'models' module, but not 'fileds' module (i.e. models.BooleanField instead of fields.BooleanField)

Sign up to request clarification or add additional context in comments.

2 Comments

I am using tastypie. I am not building model I am using an external api for a json response. Also is ArrayField dynamic in size? Is there no way to do a ListField?
Yes, the ArrayField is dynamic. Initialize as a list times = ArrayField(models.DateTimeField(), default=list) and append to it like you normally would.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.