Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Values in nested api model are null #804

Open
aklehm opened this issue Dec 4, 2020 · 2 comments
Open

Values in nested api model are null #804

aklehm opened this issue Dec 4, 2020 · 2 comments

Comments

@aklehm
Copy link

aklehm commented Dec 4, 2020

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

@KaiveYoung
Copy link

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,attribute=lambda x:x)
})

@aklehm
Copy link
Author

aklehm commented Jan 15, 2021

Thanks for your answer. I will try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants