Hi, i'm trying to follow the steps but i'm getting a error when i try to add roles to user (UserMIxin instance) and i'm not sure how to put all together..
my_user = UserMixin()
my_user.add_roles('admin', 'superadmin')
db.session.add(my_user)
db.session.commit()
But when i create a normal user, i usually do
my_user = User(name="xxx", email="[email protected]", pass="1234")
db.session.add(my_user)
db.session.commit()
my model is like this:
class User(UserMixin):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
email = db.Column(db.String(50), nullable=False, unique=True)
password = db.Column(db.String(50), nullable=False)
def __init__(self, name, email, password, roles=None):
"""Setting params to the object."""
self.name = name
self.email = email.lower()
self.password = bcrypt.hashpw(
password.encode('utf-8'), bcrypt.gensalt())
UserMixin.__init__(self, roles)
.... other methods ...
any tips on how to do i right? :)
(im using @bepetersn fork)
Hi Leo, I finally did get a chance to look at this, and I had trouble producing your exact error. I saw some others, which we can talk about & I might know what's causing them (mentioned below). However, the good news is I have a completely functional example using your above User model code at https://github.com/bepetersn/flask-permissions-ex.
It's been a little while since I looked at this stuff, but the biggest change with my fork (re: the above-mentioned errors) is you shouldn't instantiate UserMixin now -- it's an abstract model base. So you want to use a User model like yours above. And that can cause some errors -- I got a different one from yours about not being able to set roles.
Let me know what you think.
Hi Brian,
My main problem is when i create roles and then try add it to a user.. it appears the problem :)
from app import db
from flask_permissions.models import UserMixin, Role
db.create_all()
superadmin.add_abilities(
'create_admin', 'edit_admin_user', 'delete_admin_user')
db.session.add(superadmin)
db.session.commit()
#....
first_user = UserMixin
first_user.add_roles('superadmin') ###triggers the error
db.session.add(first_user)
db.session.commit()
I assume with this line...
first_user = UserMixin
you mean this...
first_user = UserMixin()
but you really need to do this...
first_user = User()
(where User
is a UserMixin
subclass of your definition).
You need to create a user instance with your User model, which should be a subclass of UserMixin. You can't do it with UserMixin directly anymore. I'm considering this a documentation failure, so I'm presently updating the README to show the right way to actually create a user with the new code. I'm going to fix the tests with this too, so the PR will finally be ready!
Most helpful comment
I assume with this line...
first_user = UserMixin
you mean this...
first_user = UserMixin()
but you really need to do this...
first_user = User()
(where
User
is aUserMixin
subclass of your definition).You need to create a user instance with your User model, which should be a subclass of UserMixin. You can't do it with UserMixin directly anymore. I'm considering this a documentation failure, so I'm presently updating the README to show the right way to actually create a user with the new code. I'm going to fix the tests with this too, so the PR will finally be ready!