Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

9
  • that's a great response, however I get a "could not encode "BaseQuery" whenever it hits a relationship with the non-flat methods, any ideas? Commented Nov 4, 2013 at 23:29
  • 1
    @SashaB How about targeting more granularly against cases where a relationship is repeated? For example, if I have online_order and address, both with a relationship to user, but online_order also has an relationship to address. If I wanted to serialize all of this, I'd have to include address in the fields_to_expand, but I wouldn't want to redundantly serialize address due to its relationship to both user and online_order. Commented Jan 28, 2015 at 22:11
  • 2
    @BenKilah Let me guess, you're using Flask-SqlAlchemy and your models are inheriting from db.Model, not Base. If that's the case, modify for field in [x for x in dir(obj) if not x.startswith('_') and x != 'metadata']: so that it reads for field in [x for x in dir(obj) if not x.startswith('_') and x != 'metadata' and not x.startswith('query')]:. Keep in mind this solution will prevent you from having a property/relationship with the name 'query' Commented Mar 6, 2015 at 23:59
  • same way like I did, but much more complex. stackoverflow.com/questions/7102754/… Commented Jun 2, 2016 at 7:46
  • 2
    You can use my solution github.com/n0nSmoker/SQLAlchemy-serializer Commented Dec 26, 2017 at 10:31