Code Monkey home page Code Monkey logo

marshmallow-mongoengine's Introduction

marshmallow-mongoengine's People

Contributors

fabian-marquardt avatar kevin-lyn avatar lafrech avatar touilleman avatar usui22750 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

Watchers

 avatar  avatar  avatar

marshmallow-mongoengine's Issues

Flask-Restful integration with pymongo

Hi,
how can I integrate marshmallow-monoengine in my flask-restful REST Api using pymongo (MongoClient) with auth-db as database connection ? How do I load data in this scenario ? Could you provide me with an example, please ?
I followed your tutorial, but when it comes to actual mondoDB connections there seems to be no documentation on it.

extra field in modelSchema

I need an extra field for my modelSchema but mongoengine complains that fields does not exist

class UserSchema(ma.ModelSchema):
    """User Schemas."""
    password = fields.Str(required=True, load_only=True)

    class Meta:
        fields = ('id', 'username', 'email', 'verified', 'cart', 'roles')
        model = User

Document the meta options available.

I really like this extension, it works great, but the meta options are not documented anywhere and this code self.model_skip_values = getattr(meta, 'model_skip_values', DEFAULT_SKIP_VALUES) took a long time to diagnose. Namely the fact that DEFAULT_SKIP_VALUES = (None, [], {}) was causing an important field to be filtered out of my API response, and I had to trace through the stack from the top of the marshmallow library down to this final piece.

Once again, works great, but documenting this behavior may save some other folks a great deal of time.

+1 for the awesome work so far!

AttributeError: 'module' object has no attribute 'Arbitrary'

It appears that marshmallow does not have a fields.Arbitrary anymore.

from marshmallow_mongoengine import ModelSchema
class UserSchema(ModelSchema):
class Meta:
model = User

marshmallow==2.0.0
marshmallow-mongoengine==0.6.5

from marshmallow_mongoengine import ModelSchema
class UserSchema(ModelSchema):
    class Meta:
        model = User

schema = UserSchema()
data, errors = schema.load(request.params)    

class Arbitrary(SkipEmptyClass, fields.Arbitrary):
AttributeError: 'module' object has no attribute 'Arbitrary'

marshmallow-mongoengine:Output value missing 'None' field

My project uses flask+mongoengine+marshmallow. When I used marshmallow to serialize the model, the returned value lacked field, and the missing field value was None.When using Django to serialize fields, the None value is still output model.

model

class Author(db.Document):
    name = db.StringField()
    books = db.ListField(db.ReferenceField('Book'))

    def __repr__(self):
        return '<Author(name={self.name!r})>'.format(self=self)


class Book(db.Document):
    title = db.StringField()

serializers

class AuthorSchema(ModelSchema):
    class Meta:
        model = Author


class BookSchema(ModelSchema):
    class Meta:
        model = Book

author_schema = AuthorSchema()

When I do this:

not return the books field
I hope to return

{
    "name":"test1",
    "books": None
}

what should I do?

Customizing the schema

Hi there! Was looking forward to using your project -

Has something changed to break this behaviour? Following the docs: http://marshmallow-mongoengine.readthedocs.io/en/latest/tutorial.html#customizing-the-schema

ma.fields.method results in AttributeError: module 'marshmallow_mongoengine.fields' has no attribute 'method'

I can't see anything called method in fields.py or marshmallow itself. Was this functionality changed, or removed? Hoping for the former! :)

import marshmallow_mongoengine as ma
from core import models

class FruitSchema(ma.ModelSchema):
    class Meta:
        model = models.Fruit

    colour = ma.fields.method(serialize="_upper_case")

    def _upper_case(self, thing):
        return thing.colour.upper()

marshmallow==2.1.0
marshmallow-mongoengine==0.7.7

same behaviour on marshmallow==2.10.4

GenericReference._serialize() returns a bson.ObjectID instead of a str

GenericReference._serialize() returns a bson.ObjectID if it's not missing, where it should be cast to str, a la Reference._serialize(). This causes JSON serialization issues when attempting to work with GenericReferenceFields.

Applying the fix from #9 to GenericReference._serialize() would fix this issue. You may also consider having GenericReference as a subclass of Reference such that you'd get it for free.

Thanks for your consideration!

Add ModelSchema to swagger

Hi,
how can I integrate a mongoengine document / a ModelSchema into swagger with flask-restplus ?

Example:

import mongoengine as me
import marshmallow_mongoengine as ma    

class Test(me.Document):    
      order = me.IntField(required=True)
      disabled = me.BooleanField(required=False, default=False)
      core = me.BooleanField(required=True)
      type = me.StringField(required=False, default="")

class TestSchema(ma.ModelSchema):
      class Meta:
            model = Test

 ....

@test_api.route('test', methods=['GET', 'POST', 'OPTIONS'])
class TestList(Resource):
      def get(self):
          ...
      @test_api.doc(params={fields of class Test object}) <- I want to pass the field definitions of class Test into here to make them appear in swaggerUI
      def post(self):
         ....

BinaryField assigned to wrong marshmallow field

The mongoengine validation of BinaryField makes it clear that a valid field should either be of type six.binary_type or bson.Binary.

However, the marshmallow-mongoengine conversion pushes mongoengine BinaryField data into the marshmallow Integer field. This causes data to be mis-cast and validation to fail, as in the example below.

from marshmallow_mongoengine import ModelSchema
from mongoengine import BinaryField, DynamicDocument


class ExampleModel(DynamicDocument):
    binary_data = BinaryField(null=True)


class ExampleModelSchema(ModelSchema):
    class Meta:
        model = ExampleModel

image

Since there is no corresponding "Binary" field in marshmallow, I propose the conversion be switched from Integer field to Raw field.

Doing so seems to solve my problem:
image

How to add post_dump / post_load methods to a `marshmallow-mongoengine` schema?

Hi.

marshmallow-mongoengine uses Marshmallow's post_dump / post_load decorators.

The docs say:

The invocation order of decorated methods of the same type is not guaranteed. If you need to guarantee order of different processing steps, you should put them in the same processing method.

What if I'd like to add a post load processing? If I add a method to the schema that I decorate with the post_load decorator, how do I know whether it will be called before or after MongoEngine object instantiation?

From a test I just made, my own decorated method was called before object instantiation, but is this reliable? And what if I wanted to have it called afterwards?

Should marshmallow-mongoengine expose its own plugs, like this?

    @ma.post_load
    def _make_object(self, data):
        self.pre_post_load(data)
        if self.opts.model_build_obj and self.opts.model:
            return self.opts.model(**data)
        else:
            return data
        self.post_post_load(data)

Then in my subclass, I'd declare

    def pre_post_load(data):
        #whatever I want to do
        ...
        return data

Just an example to illustrate the idea. The names are dummy, and it could be more elegant with decorators as in Marshmallow.

I know the use of those decorators in marshmallow-mongoengine was blessed by @sloria, and I may be totally missing something obvious, but the way is understand things now is that using marshmallow-mongoengine takes away the possibility of using those decorators for other post treatments.

Inheritance - child classes aren't serialised properly

I'm not sure if this is a feature request or bug.

Say I have the following Documents defined:

class Company(Document):
    products = EmbeddedDocumentListField(Product)

class Product(EmbeddedDocument):
    name = StringField()

    meta = {
        'allow_inheritance': True
    }

class ClothingProduct(Product):
    fabric = StringField()

class FoodProduct(Product):
    ingredients = ListField()

and I have a schema for Company:

class CompanySchema(Schema):
    class Meta:
        model = Company

and I run the following code:

productA = ClothingProduct(name='tshirt', fabric='cotton')
productB = FoodProduct(name='sausage', ingredients=['Pork', 'sage', 'breadcrumbs'])
company = Company(products=[productA, productB])
result = CompanySchema(company).dump()

result is:

{
"products": [
        {
          "_cls": "ClothingProduct",
          "name": "tshirt"
        },
        {
          "_cls": "FoodProduct",
          "name": "sausage"
        }
      ]
}

whereas I would expect it to be:

{
"products": [
        {
          "_cls": "ClothingProduct",
          "fabric": "cotton",
          "name": "tshirt"
        },
        {
          "_cls": "FoodProduct",
          "ingredients": ['Pork', 'sage', 'breadcrumbs'],
          "name": "sausage"
        }
      ]
}

I understand why this could be the case since when we create the schema, we only have the Product class to go off, but I wondered if there was a way around this. Perhaps leveraging .__subclasses__()? I can have a closer look myself but just wanted to check if this was something that had been considered before or if it is not expected behaviour.

Cheers
Dave

ModuleNotFoundError: No module named 'marshmallow.compat'

This is the error I got from running the code example on the Readme. I tried running the tests using pytest, and I got the same error. From other repo's (marshmallow-sqlalchemy) it seems that it's a marshmallow 3.x compatibility issue.

I'm not sure how to fix this myself. Also, please, do something to enable issues on the "official" repo and push them to update the Pypi version to 0.10.

Schema load and schema dump problems

When trying to use one of these two, for load I get:

_make_object() got an unexpected keyword argument 'many'

For dump:

_remove_skip_values() got an unexpected keyword argument 'many'

Seems like the kwarg many is not handled properly.

Using a boolean field with default value 'False' sets required

Hi, great library, thanks for creating&maintaining it.

When doing a POST on a resource created by the following schema, the field 'test' will be required even though it has a default value and will mark errors as {test: ["Missing data for required field."]}

class TestDoc(Document):
    test = db.BooleanField(required=True, default=False)

class TestSchema(ModelSchema):
    class Meta:
        model_skip_values = (None, )
        model = TestDoc

I think this is due to conversion/params.py checking against false
if required and not getattr(field_me, 'default', False):
Instead should be checked against None
if required and getattr(field_me, 'default', None) is None:

SequenceField not saving

I have a model that requires an integer for the _id field, so I am using SequenceField to auto-increment.

When creating a new object from the model directly, the object will save and SequenceField auto-increments correct. But when saving from a schema, the SequenceField function is not called.

Not sure if this is a bug, or am I not implementing correctly?

class Book(me.Document):
    _id = me.SequenceField(primary_key=True, 
                           collection_name="counter", 
                           sequence_name="books")
    title = me.StringField()

class BookSchema(ModelSchema):
    class Meta:
        model = Book


""" Examples using Flask-Restful"""

parse = reqparse.RequestParser()
parse.add_argument('title')

# Saving model directly
class Book(Resource):
    def put(self):
        args = parse.parse_args()
        book = Book(title=arg['title'])
        book.save()  
        # will save as insert with the new '_id' retrieved from 'counter' collection


# Saving model using schema
book_schema = BookSchema()

class Book2(Resource):
    def put(self):
        args = parse.parse_args()
        book = book_schema.load(args)
        book.save()  
        # will not save, because `_id` is requirement is not satisfied.
        # that is, it is not automatically calling SequenceField function)

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.