yiwei yiwei - 6 months ago 27
Python Question

Django TypeError("'%s' is an invalid keyword argument for this function")

So I've seen similar questions about this error, and they all seem to be related to use cases where there are ManyToMany relationships. However, I'm getting this issue even though my model doesn't have M2M relationships, so I wanted to ask and see why this was happening.

Here is my model:

class Course(models.Model):
name = models.CharField(max_length=64)
credit = models.IntegerField
notes = models.CharField(max_length=128)
resources = models.TextField
description = models.TextField
topic = models.CharField(max_length=128)


Whenever I create a new instance of this model, I get the
TypeError
for the
credit
,
resources
, and
description
fields.

I instantiate it like so:

c = Course(
name='some name',
credit='8',
notes='N/A',
resources='no resources',
description='N/A',
topic='some topic'
)


However if I change the affected fields to
models.IntegerField(max_length=8)
and
models.TextField(max_length=8)
, then the error goes away.

Why does this happen? I was under the impression that the
max_length
parameter was optional for
TextField
; I don't even know what it means in the context of an
IntegerField
. Can someone explain this behavior, and/or what I'm doing wrong?

Answer

You are defining the fields as their classes' references, and not as an instance of those classes. It should be

class Course(models.Model):
    name = models.CharField(max_length=64)
    credit = models.IntegerField()
    notes = models.CharField(max_length=128)
    resources = models.TextField()
    description = models.TextField()
    topic = models.CharField(max_length=128)
Comments