Coder Social home page Coder Social logo

marshmallow-code / flask-marshmallow Goto Github PK

View Code? Open in Web Editor NEW
864.0 17.0 59.0 427 KB

Flask + marshmallow for beautiful APIs

Home Page: http://flask-marshmallow.readthedocs.io/

License: MIT License

Python 100.00%
python flask marshmallow rest-api python-3 sqlalchemy

flask-marshmallow's Introduction

Flask-Marshmallow

Latest version Build status Documentation marshmallow 3 compatible

Flask + marshmallow for beautiful APIs

Flask-Marshmallow is a thin integration layer for Flask (a Python web framework) and marshmallow (an object serialization/deserialization library) that adds additional features to marshmallow, including URL and Hyperlinks fields for HATEOAS-ready APIs. It also (optionally) integrates with Flask-SQLAlchemy.

Get it now

pip install flask-marshmallow

Create your app.

from flask import Flask
from flask_marshmallow import Marshmallow

app = Flask(__name__)
ma = Marshmallow(app)

Write your models.

from your_orm import Model, Column, Integer, String, DateTime


class User(Model):
    email = Column(String)
    password = Column(String)
    date_created = Column(DateTime, auto_now_add=True)

Define your output format with marshmallow.

class UserSchema(ma.Schema):
    class Meta:
        # Fields to expose
        fields = ("email", "date_created", "_links")

    # Smart hyperlinking
    _links = ma.Hyperlinks(
        {
            "self": ma.URLFor("user_detail", values=dict(id="<id>")),
            "collection": ma.URLFor("users"),
        }
    )


user_schema = UserSchema()
users_schema = UserSchema(many=True)

Output the data in your views.

@app.route("/api/users/")
def users():
    all_users = User.all()
    return users_schema.dump(all_users)


@app.route("/api/users/<id>")
def user_detail(id):
    user = User.get(id)
    return user_schema.dump(user)


# {
#     "email": "[email protected]",
#     "date_created": "Fri, 25 Apr 2014 06:02:56 -0000",
#     "_links": {
#         "self": "/api/users/42",
#         "collection": "/api/users/"
#     }
# }

Learn More

To learn more about marshmallow, check out its docs.

Project Links

License

MIT licensed. See the bundled LICENSE file for more details.

flask-marshmallow's People

Contributors

alrasheeda avatar benfasoli avatar dependabot-preview[bot] avatar dependabot-support avatar dependabot[bot] avatar elvistheking avatar gabriellins64 avatar greyli avatar jeffwidman avatar jmcarp avatar numberoverzero avatar peterschutt avatar pre-commit-ci[bot] avatar sbillion avatar singingwolfboy avatar sirosen avatar sloria avatar timgates42 avatar uncle-lv avatar woutervanoorschot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

flask-marshmallow's Issues

No finding Flask-SQLAlchemy dependency

Flask-Marshmallow is not findind my Flask-SQLAlchemy extension. Consequently, thehas_sqlaflag is allwaysFalseon application start. When I serialize an object, it always returns adictionary, instead of anSQLAlchemy Model` object.

I'm using Pyenv for creating a virtual environment.
Do I need to configure anything in order to force import Flask-SQLAlchemy?

I'm using Flask-Marshmallow v0.6.2.
Thanks for any help.

sqlalchemy. Declarative support

according to http://flask.pocoo.org/docs/0.10/patterns/sqlalchemy/ , there are two ways documented to use SQLAlchemy in flask, one is with the flask-sqlalchemy extension, the other one is just Declarative, without flask-sqlalchemy.

For me, I prefer the later one. Because it makes the Database layer much purer.

In my case, I can extract an models.py file with sqlacodegen, and do the migrations with alembic, without flask-migrate.

To do the database ops, the flask context is not always necessary. and with flask-sqlalchemy, I have to edit the auto-generated models file with all 'db.' prefix to fix the import problems. Well, it brings more cons than pros.

But I still feel the ModelSchema and HyperlinkModelSchema are great code savier.

conditionally return URLs

I'm looking to selectively return some urls based on the current state of the object and am having a heck of a time solving how to expose the state property in the Schema, do some logic and determine which URLs to return based on the object state:

The Model:

class Car(Model):
    model = Column(String)
    year = Column(String)
    running = Column(Boolean)   #'0 = not running', '1 = running'

and the schema:

class CarSchema(ma.Schema):
    class Meta:
        fields = ('model', 'year', 'running',  '_links')

    _links = ma.Hyperlinks({
        'self': ma.URLFor('car_detail', id='<id>'),
        'start': ma.URLFor('car_start', id='<id>')
        'stop': ma.URLFor('car_start', id='<id>')
    })

What i'd like to do is have the start url only returned when the 'running' property is 0, and the stop url returned when it's 1, but i'm unclear on how to accomplish this.

Marshmallow seems to have a few decorators that would seem to but how would I leverage them with flask-marshmallow?

jsonify a list of objects doesn't work

@app.route('/api/users/')
def users():
    all_users = User.all()
    result = users_schema.dump(all_users)
    return jsonify(result.data)
    # OR
    # return user_schema.jsonify(all_users)

the above example raises
ValueError: dictionary update sequence element #0 has length 3; 2 is required

Deserialization of Hyperlinks and URL fields fails

From @raj-kesavan in #7 :

Simple set up with Flask-RESTful and Flask-Marshmallow. When I navigate to /api/v1/person, I get a 500 Internal Server Error. Server logs at bottom.

class PersonSchema(ma.Schema):
    name = ma.String(required = True)
    friends = ma.Hyperlinks({
        'href': ma.AbsoluteURL('person_ep')
    })

class PersonAPI(Resource):
    def get(self):
        person, errors = PersonSchema().load({
            'name': 'Rob'
        })

api.add_resource(PersonAPI, '/api/v1/person/', endpoint = 'person_ep')

Server log

[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]   File "/usr/local/lib/python2.7/dist-packages/flask_restful/__init__.py", line 521, in dispatch_request
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]     resp = meth(*args, **kwargs)
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]   File "/var/www/html/devabacus/abacus/views.py", line 95, in get
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]     'name': 'Rob'
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]   File "/usr/local/lib/python2.7/dist-packages/marshmallow/schema.py", line 498, in load
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]     dict_class=self.dict_class
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]   File "/usr/local/lib/python2.7/dist-packages/marshmallow/fields.py", line 260, in deserialize
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69]     key = fields_dict[attr_name].attribute or attr_name
[Mon Dec 22 04:15:16 2014] [error] [client 108.162.215.69] AttributeError: 'Hyperlinks' object has no attribute 'attribute'

Can you dynamically exclude fields on dump?

I have some nested schemas:

class ParentSchema(Schema):
    id = fields.Int()
    uri = fields.Url('')
    children = fields.Nested('ChildSchema', many=True, exclude=('parent',))

class ChildSchema(Schema):
    id = fields.Int()
    f = fields.String()
    parent = fields.Nested('ParentSchema', exclude=('children',))

It all works fine except that when I'm calling dump on the schema, calling parent.children results in a ginormous database query. The object being dumped is a SqlAlechemy object, and calling its children property results in lazy loading of those children from the database. The code looks like this:

parents = Parent.all()
schema = GameSchema()
results = schema.dump(parents, many=True)

I this particular case, I only need the parent records. Loading the children is wasted effort. There are, however cases, when I do need the children to be loaded. For this reason, I can't just put load_only on the nested fields in the schema definitions.

Is there any way to tell one call to dump to skip certain fields?

How to use HyperLinkRelated when endpoint's arguments are > 1?

I am not sure if I use this correctly but my use case is that i have users(table) and subscribers(association table which maps user to another user etc..) so my endpoints are like this:
/api/users/<int:id>/subscribers/ and
/api/users/<int:id>/subscribers/<int:subscriber_id>

Looking at the code I see that kwargs = {self.url_key: key} which means i can not pass more than the url_key(docs specify this).

What i expect is something like this when i request /api/users/1

{
  'username': 'john',
  'email': '[email protected]',
  'subscribers': [
    '/api/users/1/subscribers/1',
    '/api/users/1/subscribers/4',
    ...
  ]
}

Im not expert what so ever but i understand that either i change my endpoints to:
/api/subscribers/ and /api/subscribers/<int:id> .. or?

ma.URLFor is generating query url instead of specific urls

Hey, I'm using Marshmallow + Flask-RESTful and I have this attributes for my Schema class:

_links = ma.Hyperlinks({ 'self': ma.URLFor('atividade_resource', id='<id>'), 'collection': ma.URLFor('atividades_resource') })

and on my controller I have this routes:

api.add_resource(AtividadeListView, '/v1/atividades', endpoint='atividades_resource') api.add_resource(AtividadeView, '/v1/atividades/<id>', endpoint='atividade_resource')

And my JSON output is:

_links": { "collection": "/v1/atividades", "self": "/v1/atividades?id=55024fdfe138235aeac01380" },

Self shouldn't be '/v1/atividades/55024fdfe138235aeac01380'?

Is this a bug or something wrong that I am doing?

'DummySession' object has no attribute 'query'

We have been using flask-marshmallow 0.6.0 with marshmallow-sqlalchemy 0.3.0 for some time now, and been quite happy. However, in trying to upgrade our packages we have encountered the error message above.

It appears that marshmallow-sqlalchemy is now trying to actually manage adding/merging the models with the session. Personally, I don't want that. I want marshmallow to handle the deserialization and leave it to me to decide when and how I want to add or merge the model with the session, as we have been doing for some time. I have suggested on their issue board where others had brought up the issue that it would be nice if their code just skipped the session management parts if the session was none (see https://github.com/marshmallow-code/marshmallow-sqlalchemy/issues/62). If they did that, then you wouldn't need to have a DummySession class at all. I do not know how amenable they will be to that suggestion.

The alternative is unfortunately to have to make DummySession implement methods to avoid generating errors, but this requires not just the query method but then it would appear filter_by, one, and first.

Or perhaps there is an alternative workaround that you already have in place. If so, I would be anxious to hear it.

Thanks.

Oh the full traceback is

Traceback (most recent call last):
File "tests.py", line 245, in runTest
(obj, errors) = schema.loads(example_str[name])
File "/home/davism/Src/atsdb/venv/lib/python2.7/site-packages/marshmallow/schema.py", line 564, in loads
return self.load(data, many=many, partial=partial)
File "/home/davism/Src/atsdb/venv/lib/python2.7/site-packages/marshmallow_sqlalchemy/schema.py", line 186, in load
return super(ModelSchema, self).load(data, _args, *_kwargs)
File "/home/davism/Src/atsdb/venv/lib/python2.7/site-packages/marshmallow/schema.py", line 542, in load
result, errors = self._do_load(data, many, partial=partial, postprocess=True)
File "/home/davism/Src/atsdb/venv/lib/python2.7/site-packages/marshmallow/schema.py", line 646, in _do_load
result = self._invoke_load_processors(POST_LOAD, result, many, original_data=data)
File "/home/davism/Src/atsdb/venv/lib/python2.7/site-packages/marshmallow/schema.py", line 767, in _invoke_load_processors
data=data, many=many, original_data=original_data)
File "/home/davism/Src/atsdb/venv/lib/python2.7/site-packages/marshmallow/schema.py", line 865, in _invoke_processors
data = utils.if_none(processor(data), data)
File "/home/davism/Src/atsdb/venv/lib/python2.7/site-packages/marshmallow_sqlalchemy/schema.py", line 169, in make_instance
instance = self.instance or self.get_instance(data)
File "/home/davism/Src/atsdb/venv/lib/python2.7/site-packages/marshmallow_sqlalchemy/schema.py", line 154, in get_instance
return self.session.query(
AttributeError: 'DummySession' object has no attribute 'query'

Using strict flag for schemas does not work as expected

In my schema I have various validators associated with my data elements, a handful of data elements marked as required, and everything else I just want to have type checked. In the Meta section of the schema definition I assign strict to True to indicate that, by default, I want Marshmallow to not only perform the validation but throw exceptions when it detects issues.

In addition to the schema I have two functions that are going to load external data into the schema and then use it. In function A I want Marshmallow to do the error checking and validation for me, so I will not set/change the value of the strict flag when I invoke the schema. In function B, however, I want to have control of if, when, or how the validation exceptions are thrown and as such set strict to False when I invoke the schema.

When testing function B with data that would cause the schema validation to fail, I found that Marshmallow is still throwing the validation exceptions instead of simply passing everything along to me (even though I set strict to False when invoking the schema).

If I were to flip things around (i.e. set strict to False in my schema's Meta and then set strict to True when invoking the schema) everything works just like I would expect -- when using the schema variable created with strict set to True Marshmallow does in fact throw validation errors.

Has anyone else seen this issue? Assuming that the problem I described above is in fact not desired behavior?

Support file fields

It would be awesome if flask-marshmallow supported file upload fields along with a file size validator to go with it. I think this is within the scope of flask-marshmallow.

Schema.loads().data return dictionary instead of SQLAlchemy Object

I'm trying to deserialize a json object in a POST request into an SQLAlchemy Model object.

class UserSchema(ma.Schema):
  class Meta:
    model = User
UserSerializer = UserSchema()

(inside my Flask-Restful Post method)
json = request.get_json()
user = UserSerializer.load(json).data

After the deserialization, user is an empty dictionary and not an SQLAlchemy Model object.
If I manually add the fields to the UserSchema declaration, it works, however returns a dictionary instead of a SQLAlchemy Model object:

class UserSchema(ma.Schema):
  class Meta:
    model = User
    fields = ('email', 'password', 'first_name', 'last_name', 'birth_date')

Do I need to configure anything else in my UserSchema?

I have debugged the Flask-Marshmallow initialization, and the has_sqla flag is True.

Allow UrlFor fields to fail somehow

I'm trying to render HATEOAS links for a user that may or may not have the attribute avatar_filename. When the user does not have that attribute or it is None, it would be nice to just skip the rendering of the field all together. Is it possible to do this in some way? Or if not, would this be something that I could help implement?

Possible to disable sqlalchemy warning?

I have sqlalchemy installed in my project and I'm using it for some behind the scenes sqlite3 stuff, but nothing relating to marshmallow. I do not have marshmallow-sqlalchemy installed. When I import flask-marshmallow, I get the following warning:

UserWarning: Flask-SQLAlchemy integration requires marshmallow-sqlalchemy to be installed.

Is there any way I can disable this without installing marshmallow-sqlalchemy?

AbsoluteURLFor.allow_none not honoured

Hi there, I have the following schema:

from flask_marshmallow.sqla import ModelSchema
from flask_marshmallow.fields import AbsoluteURLFor

class FooSchema(ModelSchema):
    class Meta:
        model = Foo

    bar_fk = AbsoluteURLFor("bar", some_id="<bar>", allow_none=True)

Which results in:

werkzeug.routing.BuildError: Could not build url for endpoint 'bar' with values ['allow_none']. Did you forget to specify values ['some_id']?

And when I omit allow_none=True, I get:

werkzeug.routing.BuildError: Could not build url for endpoint 'bar'. Did you forget to specify values ['some_id']?

Where presumably, my serializer fails because there is no bar for Foo and it should be None?

URLFor serialization when attribute is `None` results in AttributeError

Found by @raj-kesavan in marshmallow-code/marshmallow#114 : Serializing None with a URLFor or Hyperlinks field results in an AttributeError.

from flask import Flask
from flask_marshmallow import Marshmallow

app = Flask('testapp')

@app.route('/foo/<id>')
def foo(id):
    return '..'

ma = Marshmallow(app)

class FooSchema(ma.Schema):
    links = ma.URLFor('foo', id='<id>')

ctx = app.test_request_context()
ctx.push()

schema = FooSchema()
foo1 = {'id': 1}
print(schema.dump(foo1).data)  # {'links': '/foo/1'}

# This should raise a werkzueg BuildError, NOT an AttributeError
foo2 = {'id': None}
print(schema.dump(foo2).data)  # AttributeError: 'id' is not a valid attribute of {'id': None}

foo3 = {'id': 0}
print(schema.dump(foo3).data)  # AttributeError: 'id' is not a valid attribute of {'id': 0}
ctx.pop()

The error is due to this code: https://github.com/sloria/flask-marshmallow/blob/f08feafc397a09d01acbe54ec0e4251b12b9940c/flask_marshmallow/fields.py#L79-86

This behavior is incorrect because it is checking the truthiness of the self.get_value(attr, obj), which may return a valid (but falsy) value , e.g. 0.

I think the solution is to remove this check. Will work on a fix shortly.

EDIT: Expanded the code example to clarify the bug.

invalid syntax after installing

I've a vanilla Flask app. installed this package and added in my app.py
from flask.ext.marshmallow import Marshmallow

Traceback (most recent call last):
  File "app.py", line 5, in <module>
    from flask.ext.marshmallow import Marshmallow
  File "/venv/lib/python2.6/site-packages/flask/exthook.py", line 62, in load_module
    __import__(realname)
  File "/venv/lib/python2.6/site-packages/flask_marshmallow/__init__.py", line 21, in <module>
    from . import fields
  File "/venv/lib/python2.6/site-packages/flask_marshmallow/fields.py", line 115
    for key, value in iteritems(d)
      ^
SyntaxError: invalid syntax

Is this a python version issue? Should I be running on 3?

Add request parsing; webargs; integrate flask-smore?

Brainstorming ideas for the next iteration(s) of this package:

  • Add request parsing by integrating with webargs, which now uses marshmallow. This might be as simple as providing the use_schema decorator, which allows using a Schema for both input and output and a marshal_with decorator (just output).
  • Merge flask-apispec? Would integrate with the above request parsing module and provide automatic swagger spec generation

backref uselist=False support

Presently, attempting to use ModelSchema to model a SQLAlchemy model that contains a relationship that employs a one-to-one relationship, that is, they take advantage of the backref()'s uselist=False argument, results in an TypeError when attempting to serialize the relationship.

e.g. given something like… (note, this is pseudocode-y)

class Child(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255))

class Parent(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255))
        child_id = db.Column(db.Integer, db.ForeignKey('child.id'))

    only_child = db.relationship('Child', backref=db.backref('father', uselist=False))


class ChildSchema(ma.ModelSchema):
    class Meta:
        model = Child

class ParentSchema(ma.ModelSchema):
    class Meta:
        model = Parent

…performing a dump on a record is where the "fun starts"…

child = child.get_or_404(1)
child_schema = ChildSchema()
child_schema.dump(child).data

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-2a42b0948473> in <module>()
      1 # hagbard = registrants.get_or_404(2)
----> 2 registrant_schema.dump(hagbard).data

/env/lib/python2.7/site-packages/marshmallow/schema.pyc in dump(self, obj, many, update_fields, **kwargs)
    562             dict_class=self.dict_class,
    563             index_errors=self.opts.index_errors,
--> 564             **kwargs
    565         )
    566         result = self._postprocess(preresult, many, obj=obj)

/env/lib/python2.7/site-packages/marshmallow/marshalling.pyc in serialize(self, obj, fields_dict, many, strict, skip_missing, accessor, dict_class, index_errors, index)
    135                 field_name=key,
    136                 field_obj=field_obj,
--> 137                 index=(index if index_errors else None)
    138             )
    139             skip_conds = (

/env/lib/python2.7/site-packages/marshmallow/marshalling.pyc in call_and_store(self, getter_func, data, field_name, field_obj, index)
     54         """
     55         try:
---> 56             value = getter_func(data)
     57         except ValidationError as err:  # Store validation errors
     58             self.error_fields.append(field_obj)

/env/lib/python2.7/site-packages/marshmallow/marshalling.pyc in <lambda>(d)
    129         for attr_name, field_obj in iteritems(fields_dict):
    130             key = ''.join([self.prefix, attr_name])
--> 131             getter = lambda d: field_obj.serialize(attr_name, d, accessor=accessor)
    132             value = self.call_and_store(
    133                 getter_func=getter,

/env/lib/python2.7/site-packages/marshmallow/fields.py in serialize(self, attr, obj, accessor)
    219                 else:
    220                     return self.default
--> 221         return self._serialize(value, attr, obj)
    222 
    223     def deserialize(self, value):

/env/lib/python2.7/site-packages/marshmallow/fields.py in _serialize(self, value, attr, obj)
   1215         # else:
   1216         #     items = []
-> 1217         items = [self.keygetter(v) for v in value]
   1218 
   1219         if not items:

TypeError: 'NoneType' object is not iterable

However, changing the Parent model's relationship to be only_child = db.relationship('Child', backref='father') fixes the issue.

I suspect this is because with the uselist=False, "relationship-oriented" attributes are no longer lists (even empty ones) but are rather simply None values. I tried putting in some guards to prevent the issue if value were None (there on line 1217 where it's blowing up) but simply referencing the value variable seemed to raise the error, e.g. even a if value is not None: caused the same TypeError.

I'd be really nice if this were supported, as this is the canonical way to represent 1:1 relationships AFAIK. Many kudos to you for an already great library, and thanks for your hard work!

(Also, while I realize this issue is coming up in marshmallow "proper", and might not be your purview, it seems to be a SQLAlchemy related issue and figured it would belong best here than under the vanilla marshmallow project's issues. If this is in error, I'm happy to open this issue there or elsewhere!)

q.v.: http://docs.sqlalchemy.org/en/rel_1_0/orm/basic_relationships.html#one-to-one

URL Formatting

What is the preferred method of for formatting urls?

Current output: route?id=1
Desired output: route/1

My code:
Schema: href = URLFor('route', id='<id>')
Routes: api.add_resource(cls, 'route/<id>', endpoint='route')

@property for class Meta within schema?

Because my property is derived... is there a way for me to include this within the schema? Thanks!

It keeps giving me AttributeError: "full_name" is is not a valid field for Model...

class Model(db.Model):

    first_name = String()
    last_name = String()

    @property
    def full_name(self):
        return self.first_name + ' ' + self.last_name

class ModelSchema(ma.Schema)

    class Meta:
        fields = (
            'first_name',
            'last_name',
            'full_name',
        )

Allow list of Hyperlinks

Currently there is no way to create an array/list of hyperlinks, as it expects a dict. A common format for hateoas is as this: https://developer.paypal.com/docs/api/#hateoas-links

"links" : [{
"href" : "https://api.sandbox.paypal.com/v1/payments/payment/PAY-2XR800907F429382MKEBWOSA",
"rel" : "self",
"method" : "GET"
}, {
"href" : "https://api.sandbox.paypal.com/v1/payments/payment/PAY-2XR800907F429382MKEBWOSA/execute",
"rel" : "update",
"method" : "POST"

This format provides standard keys for every link, and is therefore easier to navigate.

schema.jsonify list of sqlalchemy instances incompatible with flask.jsonify

@ElvisTheKing @sloria

continuing where #19 left off...

I am trying to use flask-marshmallow schema.jsonify on the result of a sqlalchemy query

Using model instance is ok

from models.equipment import Equipment
equipments = Equipment.query.all()
equipments_schema.jsonify(equipments[0]).response

#('{\n  "date_added": "2015-10-23T11:07:46.384414+00:00", \n  "id": 1, \n  "name": "beacon"\n}', '\n')

List of model instances breaks

equipments_schema.jsonify(equipments)

#*** AttributeError: "id" is not a valid field for [<app.models.equipment.Equipment object at 0x7f5b3c11c240>, <app.models.equipment.Equipment object at 0x7f5b3c11cba8>].

versions

Flask==0.10.1
flask-marshmallow==0.6.2
-e git+https://github.com/flask-restful/flask-restful.git@54c51fd88759e2f398d566040200d2c9d51b8e5f#egg=Flask_RESTful-master
Flask-SQLAlchemy==2.0
marshmallow==2.1.3
marshmallow-sqlalchemy==0.6.0

I think it's because the sqlalchemy query returns a list of model instances, which is not compatible with flask.jsonify. There's some discussion in flask about this, but it sounds like that'll never be supported.

Am I missing something here? from the examples in the readme one might assume this should work.

Otherwise, any thoughts for how to improve this?

Circular imports

I have a Flask app that has a structure like the following:

project
  -- users
    __init__.py
    models.py
    views.py
    serializers.py
  -- genres
    __init__.py
    models.py
    views.py
    serializers.py

I have a ModelSchema class in one of my folders that uses models.py from the other folder. SQLAlchemy by default solves this problem by using the string name of the class but I get circular import errors in Marshmallow SQLAlchemy. Is there a way to solve this? Or shoud I change my structure?

`BIT` column type raises NotImplementedError

Using SQLAlchemy to process MySQL tables for an API I'm building, and one of the columns in my table happens to be type 'BIT'. This apparently isn't supported Marshmallow, but is a valid column type for SQLAlchemy's MySQL dialect. Any chance of a fix, or is there an easier work around in the Marshmallow-SQLAlchemy connector that I'm missing?

HyperlinkModelSchema missing?

HyperlinkModelSchema is mentioned in the docs and in docstrings but doesn't seem to actually exist anywhere. Not in this project nor marshmallow-sqlalchemy. Perhaps the documentation should be updated to refer to HyperlinkRelated instead?

Enquiry : length validation

hi,

When i used Load, and the value of the variable > size in the Model column, the object is return as json.

Does Flask-marshmallow allow the validation of the variable size and return the error if it exceed the size of the Model?

Don't overwrite marshmallow URL

Currently you are overwriting marshmallow's own URL field. I don't think this is a good choice generally because in my case I want to validate a URL (so I have an input schema) and obviously I don't have an endpoint to provide it with in this case. It doesn't even make sense to have URLs require endpoints for input/validation.

Perhaps you should call the URL within flask-marshmallow differently?

AttributeError: 'Marshmallow' object has no attribute 'ModelSchema'

Using the latest 0.7.0. I installed the build on a different machine and I only get this error on this machine.

Upon inspecting the module, marshmallow indeed does NOT have a ModelSchema. Is this a bug or have I screwed something up? I haven't upgraded anything and I don't get this error on a different machine (both are running the latest Ubuntu 16 LTS)

Out[2]: <flask_marshmallow.Marshmallow at 0x7f96d8f2a9b0>

In [3]: vars(marshmallow)
Out[3]: 
{'AbsoluteURLFor': flask_marshmallow.fields.AbsoluteURLFor,
 'AbsoluteUrlFor': flask_marshmallow.fields.AbsoluteURLFor,
 'Bool': marshmallow.fields.Boolean,
 'Boolean': marshmallow.fields.Boolean,
 'Constant': marshmallow.fields.Constant,
 'Date': marshmallow.fields.Date,
 'DateTime': marshmallow.fields.DateTime,
 'Decimal': marshmallow.fields.Decimal,
 'Dict': marshmallow.fields.Dict,
 'Email': marshmallow.fields.Email,
 'Field': marshmallow.fields.Field,
 'Float': marshmallow.fields.Float,
 'FormattedString': marshmallow.fields.FormattedString,
 'Function': marshmallow.fields.Function,
 'Hyperlinks': flask_marshmallow.fields.Hyperlinks,
 'Int': marshmallow.fields.Integer,
 'Integer': marshmallow.fields.Integer,
 'List': marshmallow.fields.List,
 'LocalDateTime': marshmallow.fields.LocalDateTime,
 'Method': marshmallow.fields.Method,
 'Nested': marshmallow.fields.Nested,
 'Number': marshmallow.fields.Number,
 'Raw': marshmallow.fields.Raw,
 'Schema': flask_marshmallow.schema.Schema,
 'Str': marshmallow.fields.String,
 'String': marshmallow.fields.String,
 'Time': marshmallow.fields.Time,
 'TimeDelta': marshmallow.fields.TimeDelta,
 'URL': marshmallow.fields.Url,
 'URLFor': flask_marshmallow.fields.URLFor,
 'UUID': marshmallow.fields.UUID,
 'Url': marshmallow.fields.Url,
 'UrlFor': flask_marshmallow.fields.URLFor}

Thanks.

UrlFor fields include all passed params in url.

UrlFor fields use all the passed parameters of the field in construct the url, this makes those fields unsuitable to use with apispec in the same way as other marshmallow fields, or with any information at all.

Example:

    image_url = URLFor(
        endpoint='return_image_file',
        id='<id>',
        required=True,
        description='url location of the image file.',
    )

Result:

"image_url": "/api/image/191.png?description=url+location+of+the+image+file.&required=True",

Perhaps the best way to avoid this is define a new argument in the fields: endpoint_params, for example, to store a dict with all the endpoint parameters. For backward compatibility it can look for the existence of that argument and if exists use the parameters of the dict inside, if not use the parameters of the field as is currently done.

SyntaxError: Python 2.6.6...

File "/usr/lib/python2.6/site-packages/flask_marshmallow/fields.py", line 127
for key, value in iteritems(d)
^
SyntaxError: invalid syntax

RuntimeError: working outside of application context

I'm using Flask in the structure defined here and am coming across this error:

Traceback (most recent call last):
  File "run.py", line 2, in <module>
    from app import app
  File ".../app/__init__.py", line 13, in <module>
    from monitors.controllers import monitors
  File ".../app/monitors/controllers.py", line 3, in <module>
    from .models import Monitor, monitor_schema
  File ".../app/monitors/models.py", line 16, in <module>
    monitor_schema = MonitorSchema()
  File ".../site-packages/marshmallow/schema.py", line 227, in __init__
    self.opts = self.OPTIONS_CLASS(self.Meta)
  File ".../site-packages/flask_marshmallow/__init__.py", line 56, in __init__
    meta, 'strict', current_app.config['MARSHMALLOW_STRICT']
  File ".../site-packages/werkzeug/local.py", line 338, in __getattr__
    return getattr(self._get_current_object(), name)
  File ".../site-packages/werkzeug/local.py", line 297, in _get_current_object
    return self.__local()
  File ".../site-packages/flask/globals.py", line 34, in _find_app
    raise RuntimeError('working outside of application context')
RuntimeError: working outside of application context

I'm passing through the app instance as below (which should retain the app context?):

init.py:
app = Flask(__name__)
ma = Marshmallow(app)
monitors/models.py
from app import app, ma

class MonitorSchema(ma.Schema):
    class Meta:
        fields = ('id', 'name')

monitor_schema = MonitorSchema()
monitors/controllers.py
from .models import Monitor, monitor_schema

...
monitor = Monitor.query.get(1)
return monitor_schema.dump(monitor).data

Any ideas as to where I'm going wrong? Thanks

Request parsing example?

Is this only for serializing responses, or can it be used for parsing requests as well? Can you add an example of this?

Some change in the Docs required , for security reasons json can't pass arrray

Docs to serialise SQLAlchemy with multiple rows suggest the code bellow:

users_schema = UserSchema(many=True)

@app.route('/api/users/')
def users():
    all_users = User.all()
    result = users_schema.dump(all_users)
    return jsonify(result.data)
    # OR
    # return user_schema.jsonify(all_users)

This code will actually give error because list objects can't be jsonified, (ref this)[https://github.com/pallets/flask/issues/673]

Instead the example should be:

users_schema = UserSchema(many=True)

@app.route('/api/users/')
def users():
    all_users = User.all()
    result = users_schema.dump(all_users)
    return jsonify({'data':result.data})
    # OR
    # return user_schema.jsonify({'data':result.data})

ImportError: cannot import name ForcedError

Hello,

I could be missing something, but I keep on getting the above ImportError when just running a simple example. Actually just import Marshmallow is enough:

>>> from flask_marshmallow import Marshmallow
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/renner/devel/git/ticklist-flask/flask/lib/python2.7/site-packages/flask_marshmallow/__init__.py", line 21, in <module>
    from . import fields
  File "/home/renner/devel/git/ticklist-flask/flask/lib/python2.7/site-packages/flask_marshmallow/fields.py", line 16, in <module>
    from marshmallow.exceptions import ForcedError
ImportError: cannot import name ForcedError
>>>

marshal_with decorator?

I'm considering adding a marshal_with decorator, an idea from Flask-Restful: http://flask-restful.readthedocs.org/en/latest/fields.html . I can see how it might fit some use cases (e.g. integrating with Flask-Restful or Flask-API, which expect views to return dictionaries). However, I'm not fully convinced that it adds that much convenience to be worth the added code in this package.

BuilderError when defining UrlFor for resource that doesn't exist yet.

Hi,
I'm not sure if this is a bug, but I would like to be able to define a UrlFor in a Hyperlinksfield before the endpoint passed to UrlFor is defined. For some reason, this fails for some (but not all) UrlFors referencing endpoints. I tried to override the Schema.dumps method to add the links dynamically, like so:

class UserDocumentSchema(ma.Schema):

    ...
    _links = ma.Hyperlinks({}) # or _links = None 


    def dump(self, *args, **kwargs):
        self._links = ma.Hyperlinks({
            'self': ma.UrlFor('userdocumentresource', id="<id>") # class UserDocumentResource isn't defined yet as it uses this schema
        })

        return super(UserDocumentSchema, self).dump(*args, **kwargs)

but the changes aren't seen by the dump call for some reason.

I'm using this library together with flask-restful so I'm defining my endpoints like this:

app = Flask(__name__)
api = restful.Api(app)
ma = Marshmallow(app)

from resources import UserDocumentResource # schemas are imported here

api.add_resource(UserDocumentResource, '/foo/')

Am I doing something wrong? Can I structure my code better? Or is this something that could be fixed?
thanks,

TypeError: __init__() got an unexpected keyword argument 'ordered'

Traceback (most recent call last):
File "manage.py", line 12, in
from myapp.app import create_app
File "/home/zyb/myapp/myapp/app.py", line 4, in
from myapp import api, guanli, kanban, post, prod, public, user
File "/home/zyb/myapp/myapp/api/init.py", line 2, in
from . import views
File "/home/zyb/myapp/myapp/api/views.py", line 14, in
from myapp.extensions import db, sio
File "/home/zyb/myapp/myapp/extensions.py", line 13, in
from flask_marshmallow import Marshmallow
File "/home/zyb/.pyenv/versions/myapp/lib/python3.5/site-packages/flask_mars$
mallow/init.py", line 29, in
from . import sqla
File "/home/zyb/.pyenv/versions/myapp/lib/python3.5/site-packages/flask_mars$
mallow/sqla.py", line 35, in
class ModelSchema(msqla.ModelSchema, Schema):
File "/home/zyb/.pyenv/versions/myapp/lib/python3.5/site-packages/marshmallo$
/schema.py", line 103, in new
klass.opts = klass.OPTIONS_CLASS(meta, ordered=ordered)
TypeError: init() got an unexpected keyword argument 'ordered'

marshmallow version:3.0.0b2

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.