votes up 8

%s() got an unexpected keyword argument '%s'

github stars 59414
Exception Class:

Raise code

                    if prop in property_names or opts.get_field(prop):
                        if kwargs[prop] is not _DEFERRED:
                            _setattr(self, prop, kwargs[prop])
                        del kwargs[prop]
                except (AttributeError, FieldDoesNotExist):
            for kwarg in kwargs:
                raise TypeError("%s() got an unexpected keyword argument '%s'" % (cls.__name__, kwarg))
        post_init.send(sender=cls, instance=self)

    def from_db(cls, db, field_names, values):
        if len(values) != len(cls._meta.concrete_fields):
            values_iter = iter(values)
🙏 Scream for help to Ukraine
Today, 14th August 2022, Russia continues bombing and firing Ukraine. Don't trust Russia, they are bombing us and brazenly lying in same time they are not doing this 😠, civilians and children are dying too! We are screaming and asking exactly you to help us, we want to survive, our families, children, older ones.
Please spread the information, and ask your governemnt to stop Russia by any means. We promise to work extrahard after survival to make the world safer place for all.

Ways to fix

votes up 2 votes down

Steps to reproduce:

Step 1: Create a test directory

$ mkdir test-django

Step 2: Navigate to our test directory

$ cd test-django

Step 3: Run the following command

$ pipenv shell

Step 4: Install django dependency

$ pipenv install django

Step 5: Create a new django project namely project

$ pipenv run django-admin startproject project .

Step 6: Create a new django app namely app

$ pipenv run django-admin startapp app

Step 7: Add your app to the projects file


Step 8: Add the below code to your app's file

from django.db import models

class Test(models.Model):
    first_name = models.CharField(max_length=30)
    last_name = models.CharField(max_length=30)

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

Step 9: Now, run the command

$ pipenv run python shell

It will open a python shell for you then run the code

from app.models import Test
Test(first_name='Sam', last_name='jones',work='pilot')

The above code generates the following exception:

>>> Test(first_name='Sam', last_name='Jones', work='pilot')
Traceback (most recent call last):
 File "<console>", line 1, in <module>
 File "/home/user/test-django/app/", line 14, in __init__
   super().__init__(*args, **kwargs)
 File "/home/user/PyEnvs/test-django-BkqCejI/lib/python3.9/site-packages/django/db/models/", line 503, in __init__
   raise TypeError("%s() got an unexpected keyword argument '%s'" % (cls.__name__, kwarg))
TypeError: Test() got an unexpected keyword argument 'work'

Fixed version of the code

To fix the exception don't provide the unexpected kwargs argument "work" to the Test model class. Run the below code in the python shell itself to fix the exception.

Test(first_name='Sam', last_name='jones')


The Exception is caused as we provide an kwargs argument namely "work" which the model we created wasn't expecting that argument as a result the "Test() got an unexpected keyword argument 'work'" Exception was caused.

Jun 19, 2021 umangtaneja98 answer

Add a possible fix

Please authorize to post fix