I just started doing flask restful api and trying to register a user by sending a POST request to localhost:5000/api/v1/users
. The required fields are email and password. Here's the curl request to create a user curl http://localhost:5000/api/v1/users -d "email=admin@example.com&password=password" -X POST -v
But it returns me this error: AttributeError: 'UserCreateForm' object has no attribute 'password'
Below is some snip of my code
views.py
class UserView(restful.Resource):
def post(self):
form = UserCreateForm()
if not form.validate_on_submit():
return form.errors, 422
user = User(email=form.email.data, password=form.password.data)
db.session.add(user)
db.session.commit()
return UserSerializer(user).data
forms.py
BaseModelForm = model_form_factory(Form)
class ModelForm(BaseModelForm):
@classmethod
def get_session(self):
return db.session
class UserCreateForm(ModelForm):
class Meta:
model = User
models.py ( I tired to place the UserMixin
to second, still getting same error )
class User(db.Model, UserMixin):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(64), unique=True, index=True)
password_hash = db.Column(db.String(128))
@property
def password(self):
raise AttributeError('password is not a readable attribute')
@password.setter
def password(self, password):
self.password_hash = generate_password_hash(password)
def verify_password(self, password):
return check_password_hash(self.password_hash, password)
Also, I have login_manager.user_loader
included, so I don't think it will be issue with flask login.
And is there a documentation for wtform_alchemy? I totaly don't get how it works to create the form automatically from the models.