如何在FlaskSQLAlchemy中将数据序列化为列表?

时间:2019-06-18 02:57:24

标签: sqlalchemy flask-sqlalchemy sqldatatypes flask-restful

我有以下Track表,该表旨在返回“流派”列表。但是,SQAlchemy中没有列表数据类型,因此我将“类型”声明为string

class Track(db.Model):
    __tablename__='track'

    id = db.Column(db.Integer, primary_key=True)
    created = db.Column(db.DateTime, default=func.now())
    title = db.Column(db.String(250))
    artist = db.Column(db.String(250))
    genre = db.Column(db.String(250))

    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))

    def __init__(self, title, artist, uri): 
        self.title = title
        self.artist = artist

    def serialize(self):
       return {
           'id' : self.id,
           'title': self.title,
           'artist': self.artist,
           'created': self.created.isoformat(),
           'genre': self.genre,
       }

Track与另一个表“播放列表”之间存在关系,如下所示:

class Playlist(db.Model):

    __tablename__ = 'playlist'

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(50))
    artist = db.Column(db.String(50)) # we always need one for tagging purposes
    created = db.Column(db.DateTime, default=func.now())

    user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    menu_id = db.Column(db.Integer, db.ForeignKey('menu.id'))
    consumer_id = db.Column(db.Integer, db.ForeignKey('consumer.id'))

    tracks = db.relationship('Track', 
                        secondary=playlist_tracks,
                        backref=db.backref('playlists', 
                        lazy='dynamic'),
                        uselist=True)

    def __init__(self, title, user):
        self.title = title
        self.user = user

    def serialize(self):
       return {
           'id' : self.id,
           'created' : self.created.isoformat(),
           'title': self.title,
           'artist': self.artist,
           'tracks': [item.serialize() for item in self.tracks]
       }

和次要关系表:

playlist_tracks = db.Table('playlist_tracks',
    db.Column('track_id', db.Integer, db.ForeignKey('track.id')),
    db.Column('playlist_id', db.Integer, db.ForeignKey('playlist.id')))

如果我playlist.tracks.append(track)print (playlist.serialize()),我将得到以下序列化对象:

  -playlists: [
            {
              id: 1,
              created: "2019-06-17T23:04:35.019444",
              title: "myplaylist",
            - tracks: [
                 - {
                     id: 17,
                     title: "Bryter Layter",
                     artist: "Nick Drake",
                     created: "2019-06-17T20:12:57.766740",
                     genre: "['art rock', 'british folk', 'chamber pop', 
                             'folk', 'folk rock', 'folk-pop', 'freak folk', 
                             'indie folk', 'indie pop', 'indie rock', 
                             'melancholia', 'rock', 'singer-songwriter', 'slow 
                             core', 'traditional folk']"
                   },
              }, 

以上方法有效,但是我想要一个列表,而不是我的RESTFul响应的字符串,而且我了解到,最好的解决方案是为“体裁”创建另一列(是行的列表),所以我做到了:

class Genre(db.Model):

    __tablename__ = 'genre'

    id = db.Column(db.Integer, primary_key=True)
    genre = db.Column(db.String(50))
    title = db.Column(db.String(50))
    artist = db.Column(db.String(50))

    def __init__(self, title, artist, user):
        self.title = title
        self.artist = artist
        self.user = user

    def serialize(self):
       return {
           'id' : self.id,
           'created' : self.created.isoformat(),
           'title': self.title,
           'artist': self.artist,
           'genre': self.genre
       }

问题:

现在我该如何关联上面的所有3个表(“曲目”,“播放列表”和“流派”),以请求playlist.serialized()并让我的API正确地响应流派 serialized()作为列表,像这样吗?

     -playlists: [
            {
              id: 1,
              created: "2019-06-17T23:04:35.019444",
              title: "myplaylist",
            - tracks: [
                 - {
                     id: 17,
                     title: "Bryter Layter",
                     artist: "Nick Drake",
                     created: "2019-06-17T20:12:57.766740",
                     genre: ['art rock', 'british folk', 'chamber pop', 
                             'folk', 'folk rock', 'folk-pop', 'freak folk', 
                             'indie folk', 'indie pop', 'indie rock', 
                             'melancholia', 'rock', 'singer-songwriter', 'slow 
                             core', 'traditional folk']
                   },
              }, 

0 个答案:

没有答案