AttributeError: 'NoneType' object has no attribute 'attname' (Django)
As for me, I had:
def __init__(self):
return self.title
instead of
def __str__(self):
return self.title
so I had mistyped __init__
in place of __str__
to reformulate the previous and make it more explicit:
this error is due to an __init__
method that is not referencing its parent class.
make sure your __init__
method is defined like this:
def __init__(self, *args, **kwargs):
super().__init__(*args,**kwargs)
...
It took about 90 minutes to find this.
I only found it after I had taken out of my model first the abstract supermodel, then all relationship fields, then all-but-one data fields, until only a single IntegerField
was left. The create
still didn't work.
At that point I called create
on some other simple model class MyModel2
in exactly the same test context. It worked (like the idiomatic breeze).
So what the hell was special about MyModel
??
And then it dawned on me: MyModel
had an __init__
method; most of my other models did not. So look at that. And bang your forehead: I simply had forgotten the mandatory (in Python 3 style)
super().__init__(*args, **kwargs)
Moral: Don't forget this or you may suffer from a really tough error message.
(Note: If you don't like the style of this post, I am sorry. It was required therapeutic writing for me.)