Python Requests POST error: __init__() takes 2 arguments (1 given)
Question: So I have a Flask application which uses the Flask-Restless and Flask-
SQLAlchemy modules to create an API. My GET requests are working fine, but
POST requests are not.

From models.py:

    
    
    from application import db
    
    class User(db.Model):
        __bind_key__= 'users'
        id = db.Column(db.Integer, primary_key=True)
        email = db.Column(db.String(80))
    
        def __init__(self, email):
            self.email = email
    

From application.py (API setup):

    
    
    from flask.ext.sqlalchemy import SQLAlchemy
    import flask.ext.restless
    db = SQLAlchemy(application)
    
    manager = flask.ext.restless.APIManager(application, flask_sqlalchemy_db=db)
    manager.create_api(User, methods=['GET', 'POST'])
    

Requests processes GET requests to the API fine, but when I have a POST
request like this:

    
    
    import json
    import requests
    
    url = 'http:<ip>:<port>/api/user'
    data = {'email': '1234@here.com'}
    headers = {'Content-type': 'application/json'}
    r = requests.post(url, data=json.dumps(data), headers=headers)
    

I get this error:

    
    
    ERROR in views [X:\Python27\lib\site-packages\flask_restless\views.py:413]:
    need more than 1 value to unpack
    --------------------------------------------------------------------------------
    Traceback (most recent call last):
      File "X:\Python27\lib\site-packages\flask_restless\views.py", line 409, in extract_error_messages
        left, right = str(exception).rsplit(':', 1)
    ValueError: need more than 1 value to unpack
    

I know it has something to do with the json'd email data I'm sending, but I'm
having trouble finding out what it wants exactly. Any help is appreciated!


Answer: I found the solution!

Under models.py:

    
    
    def __init__(self, email):
    

Email (as well as any other model's params) need to have =None

    
    
    def __init__(self, email=None):
    

After that, this code works

    
    
    import requests
    
    url = 'http:<ip>:<port>/api/user'
    data = {'email': '1234@here.com'}
    r = requests.post(url, json=data)
    

