marshmallow

Adding count of total rows through Marshmallow with @post_dump?

烂漫一生 提交于 2019-12-11 06:45:30
问题 I need to add the quantity of rows returned in this query: queryPostgres = db.text(""" SELECT *, COUNT(*) OVER () as RowCount FROM ( SELECT * , ( 3958.75 * acos(sin(:lat1 / 57.2958) * sin( cast(latitude as double precision) / 57.2958) + cos(:lat1 / 57.2958) * cos( cast(latitude as double precision) / 57.2958) * cos( cast(longitude as double precision) / 57.2958 - :lon1/57.2958))) as distanceInMiles FROM "job" ) zc WHERE zc.distanceInMiles < :dst ORDER BY zc.distanceInMiles LIMIT :per_page

top-level marshmallow schema validation

淺唱寂寞╮ 提交于 2019-12-07 09:30:34
问题 From Marshmallow#validation, I know I can register validators on specific fields in a Schema. If a validator fails, errors in : data, errors = MySchema().load({"some":"data}) will include error information for any field which has failed validators : errors # => some error message for the field that failed My question : Is it possible to validate at the Schema level (rather than at individual field level) and still return an error in the above way? As an arbitrary example, say I wanted to

Why is “Object of type Decimal is not JSON serializable” - when using marshmallow with SQLAlchemy automap?

泪湿孤枕 提交于 2019-12-06 16:43:38
Using automap_base from sqlalchemy.ext.automap to map my tables. Not able to shema.dumps(result) ; getting raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type Decimal is not JSON serializable Tried using JSON custom decoders, but no use. from flask import Flask from flask_sqlalchemy import SQLAlchemy from sqlalchemy.orm import Session from sqlalchemy.ext.automap import automap_base from flask_marshmallow import Marshmallow app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///database.db' db = SQLAlchemy(app) ma = Marshmallow(app) engine = db

Python (flask/ marshmallow)ValueError: too many values to unpack (expected 2)

空扰寡人 提交于 2019-12-06 04:35:50
I am working on a Flask project and I am using marshmallow to validate user input. Below is a code snippet: def create_user(): in_data = request.get_json() data, errors = Userschema.load(in_data) if errors: return (errors), 400 fname = data.get('fname') lname = data.get('lname') email = data.get('email') password = data.get('password') cpass = data.get('cpass') When I eliminate the errors part, the code works perfectly. When I run it as it is, I get the following error: builtins.ValueError ValueError: too many values to unpack (expected 2) Traceback (most recent call last) File "/home/.

top-level marshmallow schema validation

*爱你&永不变心* 提交于 2019-12-05 16:36:16
From Marshmallow#validation , I know I can register validators on specific fields in a Schema. If a validator fails, errors in : data, errors = MySchema().load({"some":"data}) will include error information for any field which has failed validators : errors # => some error message for the field that failed My question : Is it possible to validate at the Schema level (rather than at individual field level) and still return an error in the above way? As an arbitrary example, say I wanted to validate that you tried to MySchema().load() n distinct keys. I currently have a @pre_load method which

Using Marshmallow without repeating myself

你。 提交于 2019-12-05 02:21:50
According to the official Marshmallow docs, it's recommended to declare a Schema and then have a separate class that receives loaded data, like this: class UserSchema(Schema): name = fields.Str() email = fields.Email() created_at = fields.DateTime() @post_load def make_user(self, data): return User(**data) However, my User class would look something like this: class User: def __init__(name, email, created_at): self.name = name self.email = email self.created_at = created_at This seems like repeating myself unnecessarily and I really don't like having to write the attribute names three more

How can I serialize a MongoDB ObjectId with Marshmallow?

狂风中的少年 提交于 2019-12-03 15:08:00
I'm building and API on top of Flask using marshmallow and mongoengine. When I make a call and an ID is supposed to be serialized I receive the following error: TypeError: ObjectId('54c117322053049ba3ef31f3') is not JSON serializable I saw some ways with other libraries to override the way the ObjectId is treated. I haven't figured it out with Marshmallow yet, does anyone know how to do that? My model is: class Process(db.Document): name = db.StringField(max_length=255, required=True, unique=True) created_at = db.DateTimeField(default=datetime.datetime.now, required=True) My serializer: class

SQLAlchemy @property causes 'Unknown Field' error in Marshmallow with dump_only

江枫思渺然 提交于 2019-12-02 07:24:36
I'm using flask-marshmallow (marshmallow=v3.0.0rc1, flask-marshmallow=0.9.0) and flask-sqlalchemy (sqlalchemy=1.2.16, flask-sqlalchemy=2.3.2) I have this model and schema. from marshmallow import post_load, fields from .datastore import sqla_database as db from .extensions import marshmallow as ma class Study(db.Model): _id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) tests = db.relationship("Test", backref="study", lazy='select', cascade="all, delete-orphan") @property def test_count(self): return len(self.tests) class StudySchema(ma.ModelSchema): test

How to use marshmallow to serialize a custom sqlalchemy field?

无人久伴 提交于 2019-11-30 17:32:18
问题 I just start a simple project called flask_wiki this days and I'm using some flask extensions as the follows: Flask-SQLAlchemy Flask-Restful MarshMallow Well, I just discovered that the MarshMallow project provides a class called 'ModelSchema', which reads all fields from my SQLAlchemy Model and provide a fully automated (de)serialializer. In my case, I created a 'GUID' Field which is RDBM agnostic and inserted it on my Sqlalchemy model as follows: from flask.ext.sqlalchemy import SQLAlchemy