0

i'm trying to retrieve question from my question table but i'm getting error that questions class object not iterable. the sample of code which is giving this error is

def get(self):
    for quser in session.query(questions).all():

        return jsonify(quser)

what do i need to do here. I have searched a lot on this but unable to solve it. Please help.

Manish Kumar
  • 75
  • 2
  • 11

1 Answers1

0

If I correctly understand what do you want...

You can implement this method for your class:

def to_dict(self):
    if getattr(self, '_sa_free', None):
        return {k: v for k, v in self.__dict__.iteritems()}
    attr_names = getattr(self.__class__, '__attr_names', None)
    if not attr_names:
        attr_names = [prop.key for prop in
                      class_mapper(self.__class__).iterate_properties if
                      isinstance(prop, ColumnProperty) and not prop.key.startswith('_')]
        for attr in dir(self.__class__):
            if isinstance(getattr(self.__class__, attr), (InstrumentedAttribute, property)) \
                    and not attr.startswith('_'):
                attr_names.append(attr)
        setattr(self.__class__, '__attr_names', list(set(attr_names)))

    return {attr: getattr(self, attr) for attr in attr_names}

And then:

return jsonify(**quser.to_dict())
antonio_antuan
  • 1,293
  • 9
  • 22