Skip to content

Values in nested api model are null #804

Open
@aklehm

Description

@aklehm

Hello,
I hope you can help me.
Im using python 3.9.0, Flask 1.1.2, flask-restplus 0.13.0, Flask_SQLAlchemy2.4.4 and SQLAlchemy 1.3.20.

I have a problem with a nested api model, which looks like this:

lightstate = api.model('LightState', {
    'red': fields.Integer(attribute='red', description='red part of the rgb light'),
    'green': fields.Integer(attribute='green', description='green part of the rgb light'),
    'blue': fields.Integer(attribute='blue', description='blue part of the rgb light'),
    'bri': fields.Integer(attribute='bri', description='brightness of the light')    
})
lights = api.model('Lights',{
    'id': fields.Integer(readOnly=True, description='The database id of the light'),
    'name': fields.String(required=True, description='light name'),
    'state': fields.Nested(lightstate)
})

This is my my request handler:

namespace = api.namespace('light'')

@namespace.route('/')
class Light(Resource):
    @api.marshal_with(lights)
    def get(self):
        light_data = Lights.query.all()
        return light_data

And my dto:

db = SQLAlchemy()

class Lights(db.Model):
    __bind_key__ = 'lights'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255))
    red = db.Column(db.Integer)
    green = db.Column(db.Integer)
    blue = db.Column(db.Integer)
    bri = db.Column(db.Integer)

    def __init__(self, name, red, green, blue, bri):
        self.name = name
        self.red = red
        self.green = green
        self.blue = blue
        self.bri = bri

If I send a request, I'm getting this response with null values:

[
  {
    "id": 1,
    "name": "garden",
    "state": {
      "red": null,
      "green": null,
      "blue": null,
      "bri": null
    }
  },
  {
    "id": 2,
    "name": "pool",
    "state": {
      "red": null,
      "green": null,
      "blue": null,
      "bri": null
    }
  }
]

But if I remove the nested field from my api model 'lights' and add all fields, the values are not null.

lights = api.model('Lights', {
    'id': fields.Integer(readOnly=True, description='The database id of the light'),
    'name': fields.String(required=True, description='light name'),
    'red': fields.Integer(description='red part of the rgb light'),
    'green': fields.Integer(description='green part of the rgb light'),
    'blue': fields.Integer(description='blue part of the rgb light'),
    'bri': fields.Integer(description='brightness of the light')
})
[
  {
    "id": 1,
    "name": "garden",
    "red": 50,
    "green": 205,
    "blue": 50,
    "bri": 84
  },
  {
    "id": 2,
    "name": "pool",
    "red": 255,
    "green": 42,
    "blue": 255,
    "bri": 96
  }
]

I want to use the nested fields. How do I get the values in my response?

Thanks in advance

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions