0

I have a flask app with database. I need to localize some types of entities there. The problem is, i am using Flask-Marshmallow for parsing/serializing my database objects. And there is an endpoint, that grabs the root object (User) and all it related nested objects (Match), which in it's turn grab it's related objects (Question), which i need to localize. So, having Accept-Language header can't be directly passed to nested->nested (Question) object. It's some architecture issue, i believe, not a technical one. If someone ever face such a problem and know any workarounds, please let me know.

# views.py
@app.route('/self', methods=['GET'])
@auth.login_required
def get_user:
    user = g.user
    schema = UserSchema()
    res = schema.dumps(user)

# UserSchema.py
class UserSchema(marshmallow.Schema):
    id = fields.Int()
    matches = fields.Nested('MatchSchema', many=True)
    // etc.
Artem Zaytsev
  • 1,621
  • 20
  • 19
  • How about write a custom function to transform the data object to dict? – stamaimer Mar 30 '17 at 15:01
  • Well, that would be a lot of work to do and a lot of places to make a mistake. I'd like to keep code clean and non-repetitive as possible. And marshmallow help a lot to avoid this routing code. I've found out that i could use "request context" to pass locale parameters deeper. Also i can reimplement get_attribute function to get locale and force parser to find a translation before dumping responce. – Artem Zaytsev Mar 31 '17 at 06:59

0 Answers0