Reputation: 24121
I have a model:
class User(models.Model):
user_id = models.IntegerField(default=0)
user_name = models.CharField(max_length=20)
user_age = models.IntegerField(default=0)
def __init__(self, user_id, user_name, user_age):
self.user_id = user_id
self.user_name = user_name
self.user_age = user_age
And then I try to create an instance of this model:
new_user = User(0, 'Andrew', 25)
new_user.save()
But this gives me the error:
'User' object has no attribute '_state'
What does this mean?
Upvotes: 1
Views: 1134
Reputation: 599630
You should not really be defining the __init__
method at all. falsetru shows that you need to call the super class method, but all the functionality you've put in your own version is already provided by that.
Upvotes: 3
Reputation: 369134
The code is missing call to super class' __init__
:
def __init__(self, user_id, user_name, user_age):
super(User, self).__init__() # <----
self.user_id = user_id
self.user_name = user_name
self.user_age = user_age
Upvotes: 5