3

I am using flask-restful this is My class I want to insert

class OrderHistoryResource(Resource):

    model = OrderHistoryModel
    schema = OrderHistorySchema
    order = OrderModel
    product = ProductModel

    def post(self):
        value = req.get_json()
        data = cls.schema(many=True).load(value)  
        data.insert()

In my model

def insert(self):
    db.session.add(self)
    db.session.commit()

schema

from config.ma import ma
from model.orderhistory import OrderHistoryModel

class OrderHistorySchema(ma.ModelSchema):
    class Meta:
        model = OrderHistoryModel
        include_fk = True

Example Data I want to insert

[
    {
        "quantity":99,
        "flaskSaleStatus":true,
        "orderId":"ORDER_64a79028d1704406b6bb83b84ad8c02a_1568776516",
        "proId":"PROD_9_1568779885_64a79028d1704406b6bb83b84ad8c02a"
     },
     {
        "quantity":89,
        "flaskSaleStatus":true,
        "orderId":"ORDER_64a79028d1704406b6bb83b84ad8c02a_1568776516",
        "proId":"PROD_9_1568779885_64a79028d1704406b6bb83b84ad8c02a"
     }
]

this is what i got after insert method has started

TypeError: insert() takes exactly 2 arguments (0 given)

or there is another way to do this action?

21bn
  • 117
  • 1
  • 2
  • 12
  • You seem to have a few strange features in this code. You don't need to define `post` as a class method - and inside that method you've passed `self` in the function definition but use `cls` to reach the schema, seems like this should error. I can't see the rest of the code but assume `OrderHistorySchema` is just a marshmallow schema? Which means `data` isn't an instance of your model therefore might be a different `insert` method? Please show model and schema classes. – elembie Sep 19 '19 at 05:27
  • sorry for not clear question this happended cause i changed schema() to schema(many=True) when Insert method started (in model) seem like it didn't see any parameters – 21bn Sep 19 '19 at 06:06

2 Answers2

9

Edited - released marshmallow-sqlalchemy loads directly to instance

You need to loop through the OrderModel instances in your list.

You can then use add_all to add the OrderModel objects to the session, then bulk update - see the docs

Should be something like:

db.session.add_all(data)
db.session.commit() 

See this post for brief discussion on why add_all is best when you have complex ORM relationships.

Also - not sure you need to have all your models/schemas as class variables, it's fine to have them imported (or just present in the same file, as long as they're declared before the resource class).

elembie
  • 600
  • 2
  • 8
0

You are calling insert on list cause data is list of model OrderHistoryModel instances.

Also post method doesn't need to be classmethod and you probably had an error there as well.

Since data is list of model instances you can use db.session.add_all method to add them to session in bulk.

    def post(self):
        value = req.get_json()
        data = self.schema(many=True).load(value)
        db.session.add_all(data)
        db.session.commit() 
stasiekz
  • 1,775
  • 5
  • 22