Python Forum

Full Version: TypeError: __init__() missing 1 required positional argument: 'instructions'
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
So every time I try to POST, i get an error.

Error:
Traceback (most recent call last): File "C:\Users\Chris\.virtualenvs\Backend-NKSSbLU1\Lib\site-packages\flask\app.py", line 1498, in __call__ return self.wsgi_app(environ, start_response) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Chris\.virtualenvs\Backend-NKSSbLU1\Lib\site-packages\flask\app.py", line 1476, in wsgi_app response = self.handle_exception(e) ^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Chris\.virtualenvs\Backend-NKSSbLU1\Lib\site-packages\flask\app.py", line 1473, in wsgi_app response = self.full_dispatch_request() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Chris\.virtualenvs\Backend-NKSSbLU1\Lib\site-packages\flask\app.py", line 882, in full_dispatch_request rv = self.handle_user_exception(e) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Chris\.virtualenvs\Backend-NKSSbLU1\Lib\site-packages\flask\app.py", line 880, in full_dispatch_request rv = self.dispatch_request() ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Chris\.virtualenvs\Backend-NKSSbLU1\Lib\site-packages\flask\app.py", line 865, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return] ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\Chris\Desktop\MaintTracker\Backend\app.py", line 174, in add_task new_task = Task(job, instructions) ^^^^^^^^^^^^^^^^^^^^^^^ TypeError: __init__() missing 1 required positional argument: 'instructions'
this is the code I'm using,


class Task(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    job = db.Column(db.String, unique=False)
    instructions = db.Column(db.String, unique=False)

    def __init__(self,id,job,instructions):
        self.id = id
        self.job = job
        self.instructions = instructions
        
       
        



class TaskSchema(ma.Schema):
    class Meta:
        fields = ('id','job', 'instructions' )


task_schema = TaskSchema()
tasks_schema = TaskSchema(many=True)


@app.route("/Task", methods=["POST"])
def add_task():
    
    job = request.json['job']
    instructions = request.json['instructions']
    
    new_task = Task(job, instructions)

    db.session.add(new_task)
    db.session.commit()

    task = Task.query.get(new_task.id)

    return task_schema.jsonify(task)
I also have other tables in this program with nearly identical structure that do work.
I do not know how to fix it, but
in Line 31 You supply 2 arguments when You create the Task Objekt, You have to give three arguments, because
Task.__init__ has got 3 parameters (id,job,instructions), so the third parameter is missing.
(Jul-01-2024, 03:13 PM)gerpark Wrote: [ -> ]I do not know how to fix it, but
in Line 31 You supply 2 arguments when You create the Task Objekt, You have to give three arguments, because
Task.__init__ has got 3 parameters (id,job,instructions), so the third parameter is missing.

I know, but if I add the id arg it gives me a keyerror:'id'. It was working for months and then I added a column to a different table and grew a bunch of issues after recreating the db. The id is the primary key and should have been auto incrementing.
As far as I understood primary key/autoincrement columns must not be passed to the constructor,
so the constructor for the Task should be this way:

def __init__(self,job,instructions):
https://stackoverflow.com/questions/1925...-method-vs
In newer Flask or for long time no need for __init__ when create a database.
So just this.
class Task(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    job = db.Column(db.String, unique=False)
    instructions = db.Column(db.String, unique=False)