{"action":"created","issue":{"url":"https://api.github.com/repos/dpgaspar/Flask-AppBuilder/issues/171","labels_url":"https://api.github.com/repos/dpgaspar/Flask-AppBuilder/issues/171/labels{/name}","comments_url":"https://api.github.com/repos/dpgaspar/Flask-AppBuilder/issues/171/comments","events_url":"https://api.github.com/repos/dpgaspar/Flask-AppBuilder/issues/171/events","html_url":"https://github.com/dpgaspar/Flask-AppBuilder/issues/171","id":77620341,"number":171,"title":"Overriding Role security model relation error","user":{"login":"jvinolas","id":2132863,"avatar_url":"https://avatars.githubusercontent.com/u/2132863?v=3","gravatar_id":"","url":"https://api.github.com/users/jvinolas","html_url":"https://github.com/jvinolas","followers_url":"https://api.github.com/users/jvinolas/followers","following_url":"https://api.github.com/users/jvinolas/following{/other_user}","gists_url":"https://api.github.com/users/jvinolas/gists{/gist_id}","starred_url":"https://api.github.com/users/jvinolas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jvinolas/subscriptions","organizations_url":"https://api.github.com/users/jvinolas/orgs","repos_url":"https://api.github.com/users/jvinolas/repos","events_url":"https://api.github.com/users/jvinolas/events{/privacy}","received_events_url":"https://api.github.com/users/jvinolas/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-05-18T11:33:10Z","updated_at":"2015-05-20T07:33:22Z","closed_at":null,"body":"I need to add fields to Role security model so I did it like in User model example. The problem is that there is the permission relation that is not @declared_attr in Role model:\r\n```\r\npermissions = relationship('PermissionView', secondary=assoc_permissionview_role, backref='role')\r\n```\r\nso I can't implement that as I did in User model. The error comes when Fabmanager create-admin tries to add default 'admin' user:\r\n```\r\nERROR:flask_appbuilder.security.sqla.manager:Error adding new user to database. Attempting to flush an item of type <class 'app.security.security_models.MyRole'> as a member of collection \"User.roles\". Expected an object of type <class 'flask_appbuilder.security.sqla.models.Role'> or a polymorphic subclass of this type. If <class 'app.security.security_models.MyRole'> is a subclass of <class 'flask_appbuilder.security.sqla.models.Role'>, configure mapper \"Mapper|Role|ab_role\" to load this subtype polymorphically, or set enable_typechecks=False to allow any subtype to be accepted for flush. \r\n```\r\nIs that the problem or there is another way of implementing MyRole?\r\n\r\nThis is what I've done. First I subclassed the Role model with my new fields:\r\n```\r\nclass MyRole(Role):\r\n\tquota_v = Column(Integer)\r\n\tquota_r = Column(Integer)\r\n\tquota_t = Column(Integer)\r\n```\r\nThen  the view:\r\n```\r\nclass MyRoleModelView(RoleModelView):\r\n    route_base = '/roles'\r\n\r\n    list_title = lazy_gettext('List Roles')\r\n    show_title = lazy_gettext('Show Role')\r\n    add_title = lazy_gettext('Add Role')\r\n    edit_title = lazy_gettext('Edit Role')\r\n\r\n    label_columns = {'name': lazy_gettext('Name'), 'permissions': lazy_gettext('Permissions')}\r\n    list_columns = ['name', 'permissions','quota_vm','quota_run','quota_tmpl']\r\n    show_columns = ['name', 'permissions']\r\n    order_columns = ['name']\r\n    search_columns = ['name']\r\n```\r\nAnd overrided in security.py:\r\n```\r\nclass MySecurityManager(SecurityManager):\r\n\tuser_model = MyUser\r\n\tuserdbmodelview = MyUserDBModelView\r\n\trole_model = MyRole\r\n\trolemodelview = MyRoleModelView\r\n\tuserldapmodelview = MyUserLDAPModelView\r\n```"},"comment":{"url":"https://api.github.com/repos/dpgaspar/Flask-AppBuilder/issues/comments/103794077","html_url":"https://github.com/dpgaspar/Flask-AppBuilder/issues/171#issuecomment-103794077","issue_url":"https://api.github.com/repos/dpgaspar/Flask-AppBuilder/issues/171","id":103794077,"user":{"login":"jvinolas","id":2132863,"avatar_url":"https://avatars.githubusercontent.com/u/2132863?v=3","gravatar_id":"","url":"https://api.github.com/users/jvinolas","html_url":"https://github.com/jvinolas","followers_url":"https://api.github.com/users/jvinolas/followers","following_url":"https://api.github.com/users/jvinolas/following{/other_user}","gists_url":"https://api.github.com/users/jvinolas/gists{/gist_id}","starred_url":"https://api.github.com/users/jvinolas/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jvinolas/subscriptions","organizations_url":"https://api.github.com/users/jvinolas/orgs","repos_url":"https://api.github.com/users/jvinolas/repos","events_url":"https://api.github.com/users/jvinolas/events{/privacy}","received_events_url":"https://api.github.com/users/jvinolas/received_events","type":"User","site_admin":false},"created_at":"2015-05-20T07:33:22Z","updated_at":"2015-05-20T07:33:22Z","body":"The last thing I had to do is create another relation between MyRoles and MyUsers. That is because the roles relation is not shared among MyUser class:\r\n\r\n```\r\nclass User(Model):\r\n    __tablename__ = 'ab_user'\r\n    roles = relationship('Role', secondary=assoc_user_role, backref='user')\r\n```\r\nSo I had to add my own relation to my subclass models:\r\n\r\n```\r\nfrom flask_appbuilder.security.sqla.models import User, Role, assoc_user_role\r\nclass MyUser(User):\r\n\tmyroles = relationship('MyRole', secondary=assoc_user_role, backref='myuser')\r\n```\r\nBecause without that relation that is not shared from User model I got this error when I tried to show or edit a role: (appbuilder tries to get related view relation name and can't find it)\r\n```\r\n2015-05-20 09:14:59,828:ERROR:app:Exception on /roles/edit/3 [GET]\r\nTraceback (most recent call last):\r\n  File \"/opt/dovirt/env/lib/python2.7/site-packages/flask/app.py\", line 1817, in wsgi_app\r\n    response = self.full_dispatch_request()\r\n  File \"/opt/dovirt/env/lib/python2.7/site-packages/flask/app.py\", line 1477, in full_dispatch_request\r\n    rv = self.handle_user_exception(e)\r\n  File \"/opt/dovirt/env/lib/python2.7/site-packages/flask/app.py\", line 1381, in handle_user_exception\r\n    reraise(exc_type, exc_value, tb)\r\n  File \"/opt/dovirt/env/lib/python2.7/site-packages/flask/app.py\", line 1475, in full_dispatch_request\r\n    rv = self.dispatch_request()\r\n  File \"/opt/dovirt/env/lib/python2.7/site-packages/flask/app.py\", line 1461, in dispatch_request\r\n    return self.view_functions[rule.endpoint](**req.view_args)\r\n  File \"/opt/dovirt/env/lib/python2.7/site-packages/flask_appbuilder/security/decorators.py\", line 27, in wraps\r\n    return f(self, *args, **kwargs)\r\n  File \"/opt/dovirt/env/lib/python2.7/site-packages/flask_appbuilder/views.py\", line 513, in edit\r\n    widgets = self._edit(pk)\r\n  File \"/opt/dovirt/env/lib/python2.7/site-packages/flask_appbuilder/baseviews.py\", line 852, in _edit\r\n    orders=orders, pages=pages, page_sizes=page_sizes, widgets=widgets)\r\n  File \"/opt/dovirt/env/lib/python2.7/site-packages/flask_appbuilder/baseviews.py\", line 660, in _get_related_views_widgets\r\n    view.__class__.__name__)))\r\n  File \"/opt/dovirt/env/lib/python2.7/site-packages/flask_appbuilder/baseviews.py\", line 634, in _get_related_view_widget\r\n    log.error(\"Can't find relation on related view {0}\".format(related_view.name))\r\nAttributeError: 'MyUserDBModelView' object has no attribute 'name'\r\n```\r\nI still think that role relation must be @declared_attr to avoid having to create my own relation between MyUser and MyRole models."}}