TypeError: __init__() missing 1 required positional argument: 'instructions'

Cant figure out why I’m getting this error.

127.0.0.1 - - [01/Jul/2024 09:54:58] "POST /Task HTTP/1.1" 500 -
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'

here is my code.

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=["GET"])
def get_tasks():
    all_task = Task.query.all()
    result = tasks_schema.dump(all_task)
    return jsonify(result)

@app.route("/Task/<id>", methods=["GET"])
def get_task(id):
    task = Task.query.get(id)
    return task_schema.jsonify(task)

@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’ve removed and replaced and hand typed over auto complete and have no clue what the issue is.

    new_task = Task(job, instructions)
               ^^^^^^^^^^^^^^^^^^^^^^^
TypeError: __init__() missing 1 required positional argument: 'instructions'

Error means one more argument is required to create new instance of the Task class.

I get that, but where? the init has the required argument.

I also have other tables in this program with nearly identical structure that do work.

it is saying where, in app.py on line 174

this is line 174: new_task = Task(job, instructions)

and it has only two arguments, when it wants 3

but the one it is asking for is there, wouldn’t it ask for a different one?

it’s asking for the third one, which in your __init__ is instructions, you have given a value to the first two parameters, id, and job with the two values you have as arguments

1 Like

so it sounds like i should add id back in but when i do that i get key error:id

the function needs three arguments, you need to fix something else it seems

1 Like

but when i use the same method here,

@app.route("/Specs", methods=["POST"])
def add_specs():
    
    sn = request.json['sn']
    qrcode = request.json['qrcode']
    name = request.json['name']
    designator = request.json['designator']
    subdesignator = request.json['subdesignator']
    hours = request.json['hours']
    oil = request.json['oil']
    coolant = request.json['coolant']
    department = request.json['department']
    motor = request.json['motor']

    new_specs = Specs(qrcode,sn, name, designator, subdesignator,oil,coolant,department,motor,hours)

    db.session.add(new_specs)
    db.session.commit()

    specs = Specs.query.get(new_specs.sn)

    return specs_schema.jsonify(specs)

it works fine.

sorry that was the wrong one

what method? I don’t see creating a new instance of Task here

this is a different table but the same setup in the same file.

I don’t see how Specs is, I don’t know

class Specs(db.Model):
    id = db.Column(db.Integer, unique=True)
    qrcode = db.Column(db.String(), unique=False)
    sn = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), unique=False)
    designator = db.Column(db.String(144), unique=False)
    subdesignator = db.Column(db.String(20), unique=False)
    oil = db.Column(db.String(15), unique=False)
    coolant = db.Column(db.String(15), unique=False)
    department =db.Column(db.String(20), unique=False)
    motor = db.Column(db.String(), unique=False)
    hours = db.Column(db.String(),unique=False)


    def __init__(self,qrcode,sn, name, designator, subdesignator,oil,coolant,department,motor,hours):
        
        self.qrcode = qrcode
        self.sn = sn
        self.name = name
        self.designator = designator
        self.subdesignator = subdesignator
        self.oil = oil
        self.coolant = coolant
        self.department = department
        self.motor = motor
        self.hours = hours



class SpecsSchema(ma.Schema):
    class Meta:
        fields = ('qrcode','sn', 'name', 'designator', 'subdesignator','oil','coolant','department','motor','hours')


specs_schema = SpecsSchema()
specss_schema = SpecsSchema(many=True)

there is no id required here, you have given all the arguments necessary

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.