Python Forum
TypeError: __init__() missing 1 required positional argument: 'instructions'
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
TypeError: __init__() missing 1 required positional argument: 'instructions'
#1
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)
Reply
#2
I also have other tables in this program with nearly identical structure that do work.
Reply
#3
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.
Reply
#4
(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.
Reply
#5
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
Reply
#6
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) 
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Shocked Django __init__() got an unexpected keyword argument 'any' ikurorox 2 8,035 Nov-26-2021, 07:57 PM
Last Post: ikurorox
  Django views : positional argument ghoul 0 1,915 Nov-15-2021, 06:02 PM
Last Post: ghoul
  TypeError: to_capabilities() missing 1 required positional argument: 'self' OceansBlue 2 7,125 Dec-03-2020, 12:08 AM
Last Post: OceansBlue
  TypeError: missing 1 required positional argument (word counter Django app) Drone4four 2 17,132 Jul-11-2019, 09:34 PM
Last Post: Drone4four
  TypeError("index() missing 1 required positional argument: 'pymydb'" nikos 2 5,326 Mar-03-2019, 09:21 PM
Last Post: micseydel
  TypeError: __init__() IMuriel 7 9,057 Jan-09-2019, 09:48 PM
Last Post: nilamo
  TypeError: get_names() takes 0 positional arguments but 1 was given Truman 2 11,539 Aug-15-2018, 10:32 PM
Last Post: Truman
  TypeError: Method takes takes exactly 1 argument but 2 given pras120687 1 10,178 Dec-15-2016, 07:10 PM
Last Post: Yoriz

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020