0

I want to make an API that accepts two date parameters and have that return all results in a database table. I don't want these dates stored anywhere so I can't define them as columns. My table has three columns: id (int), answers(json), datestamp(DateTime). I want to pass in "start_date" and "end_date" variables in the API request, and have all answers and ids from that period. My date conversion from strings in fine but I keep getting hit with the {"_schema":["Invalid input type."]} response. Is it possible to pass in these fields even though they don't exist in the table? Do I need to define them like I have done the columns in my model? If so instead of Column(...), what would it be?

I've tried "additional_properties = fields.Raw()" but I still got the invalid input type error. I've played with the schema but seem to be missing something.

schema

from ma import ma # marshmallow
from models.c_bulk import CBulkModel
from flask_restful import fields
class CBulkSchema(ma.ModelSchema):
    class Meta:
        additional properties = fields.Raw()
        model = ClaraBulkModel
        dump_only = ("id", "answers", "datestamp")

model

from typing import List
from datetime import timedelta
from db import db


class CBulkModel(db.Model):
    __tablename__ = "test_post_date"
    datestamp = db.Column(db.DateTime, nullable=False)
    id = db.Column(db.BigInteger, nullable=True, unique=True, primary_key=True)
    answers = db.Column(db.JSON, nullable=True)
    @classmethod
    def find_all(cls, start_date, end_date): # -> List["CBulkModel"]:
        return cls.query.filter(db.and_(start_date >= cls.datestamp, end_date < cls.datestamp))

{"_schema":["Invalid input type."]} instead of the surveys from that time.

Community
  • 1
  • 1

2 Answers2

0

Well I guess I figure it out. There were more code changes to make the final solution work by the way but the input issue was resolved by not passing in the JSON. If you don't pass in the JSON through your schema you can load in any attribute and use it as a filter.

0

try add unknown = INCLUDE.. hope this help...

enter code herefrom ma import ma # marshmallow
from models.c_bulk import CBulkModel
from flask_restful import fields
class CBulkSchema(ma.ModelSchema):
    class Meta:
        additional properties = fields.Raw()
        model = ClaraBulkModel
        dump_only = ("id", "answers", "datestamp")
        unknown = INCLUDE