In Python Flask, how to get the role name by the current logged in user?How do I copy a file in Python?How can I safely create a nested directory in Python?How to get the current time in PythonHow can I make a time delay in Python?Getting the class name of an instance?Getting the last element of a list in PythonHow do I get the number of elements in a list in Python?How do I concatenate two lists in Python?How do I lowercase a string in Python?SQLAlchemy relationships, when to use which relationship?

Why don't electromagnetic waves interact with each other?

Why did the Germans forbid the possession of pet pigeons in Rostov-on-Don in 1941?

Why CLRS example on residual networks does not follows its formula?

What are these boxed doors outside store fronts in New York?

Why don't electron-positron collisions release infinite energy?

What do the dots in this tr command do: tr .............A-Z A-ZA-Z <<< "JVPQBOV" (with 13 dots)

How do I create uniquely male characters?

What defenses are there against being summoned by the Gate spell?

Writing rule which states that two causes for the same superpower is bad writing

Shell script not opening as desktop application

Why are only specific transaction types accepted into the mempool?

Infinite past with a beginning?

How is this relation reflexive?

Why can't I see bouncing of a switch on an oscilloscope?

Why Is Death Allowed In the Matrix?

Why was the small council so happy for Tyrion to become the Master of Coin?

Can I interfere when another PC is about to be attacked?

Can an x86 CPU running in real mode be considered to be basically an 8086 CPU?

What is the offset in a seaplane's hull?

Is there a familial term for apples and pears?

declaring a variable twice in IIFE

A function which translates a sentence to title-case

Proof for divisibility of polynomials.

Why doesn't Newton's third law mean a person bounces back to where they started when they hit the ground?



In Python Flask, how to get the role name by the current logged in user?


How do I copy a file in Python?How can I safely create a nested directory in Python?How to get the current time in PythonHow can I make a time delay in Python?Getting the class name of an instance?Getting the last element of a list in PythonHow do I get the number of elements in a list in Python?How do I concatenate two lists in Python?How do I lowercase a string in Python?SQLAlchemy relationships, when to use which relationship?






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








0















I have 3 tables for user-role many-to-many relationship in Flask. How should I get the role name from the current user?



In my models.py



class User(db.Model, UserMixin):
__tablename__ = "Users"
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(100), nullable=False, unique=True)
password = db.Column(db.String(255), nullable=False, server_default='')

# Define the relationship to Role via UserRoles
roles = db.relationship('Role', secondary='UserRoles', backref=db.backref("user", lazy="dynamic"))


# Define the Role data-model
class Role(db.Model):
__tablename__ = 'Roles'
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(50), unique=True)
users = db.relationship('User', secondary='UserRoles', backref=db.backref("role", lazy="dynamic"))


# # Define the UserRoles association table
class UserRole(db.Model):
__tablename__ = 'UserRoles'
id = db.Column(db.Integer(), primary_key=True)
user_id = db.Column(db.Integer(), db.ForeignKey('Users.id', ondelete='CASCADE'))
role_id = db.Column(db.Integer(), db.ForeignKey('Roles.id', ondelete='CASCADE'))


In my views.py:



from flask_user import current_user

@app.route("/TestUserRole")
def user_role():
role = current_user.role
return str(role)


On the browser, it shows me the returned result:



SELECT [Roles].id AS [Roles_id], [Roles].name AS [Roles_name] FROM [Roles], [UserRoles] WHERE [UserRoles].user_id = ? AND [Roles].id = [UserRoles].role_id


Is there a possible way to show the role name of the current user, something like the following (bad example, though, since it will pop out error)?



role = current_user.role.name









share|improve this question






















  • Try user_roles = [role.name for role in current_user.roles]

    – tntxtnt
    Mar 9 at 4:27











  • Can you not do something like Role.query.get(**current users role id**) and add this as an extra column? I'm new too :)

    – Andrew Allen
    Mar 11 at 16:19

















0















I have 3 tables for user-role many-to-many relationship in Flask. How should I get the role name from the current user?



In my models.py



class User(db.Model, UserMixin):
__tablename__ = "Users"
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(100), nullable=False, unique=True)
password = db.Column(db.String(255), nullable=False, server_default='')

# Define the relationship to Role via UserRoles
roles = db.relationship('Role', secondary='UserRoles', backref=db.backref("user", lazy="dynamic"))


# Define the Role data-model
class Role(db.Model):
__tablename__ = 'Roles'
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(50), unique=True)
users = db.relationship('User', secondary='UserRoles', backref=db.backref("role", lazy="dynamic"))


# # Define the UserRoles association table
class UserRole(db.Model):
__tablename__ = 'UserRoles'
id = db.Column(db.Integer(), primary_key=True)
user_id = db.Column(db.Integer(), db.ForeignKey('Users.id', ondelete='CASCADE'))
role_id = db.Column(db.Integer(), db.ForeignKey('Roles.id', ondelete='CASCADE'))


In my views.py:



from flask_user import current_user

@app.route("/TestUserRole")
def user_role():
role = current_user.role
return str(role)


On the browser, it shows me the returned result:



SELECT [Roles].id AS [Roles_id], [Roles].name AS [Roles_name] FROM [Roles], [UserRoles] WHERE [UserRoles].user_id = ? AND [Roles].id = [UserRoles].role_id


Is there a possible way to show the role name of the current user, something like the following (bad example, though, since it will pop out error)?



role = current_user.role.name









share|improve this question






















  • Try user_roles = [role.name for role in current_user.roles]

    – tntxtnt
    Mar 9 at 4:27











  • Can you not do something like Role.query.get(**current users role id**) and add this as an extra column? I'm new too :)

    – Andrew Allen
    Mar 11 at 16:19













0












0








0








I have 3 tables for user-role many-to-many relationship in Flask. How should I get the role name from the current user?



In my models.py



class User(db.Model, UserMixin):
__tablename__ = "Users"
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(100), nullable=False, unique=True)
password = db.Column(db.String(255), nullable=False, server_default='')

# Define the relationship to Role via UserRoles
roles = db.relationship('Role', secondary='UserRoles', backref=db.backref("user", lazy="dynamic"))


# Define the Role data-model
class Role(db.Model):
__tablename__ = 'Roles'
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(50), unique=True)
users = db.relationship('User', secondary='UserRoles', backref=db.backref("role", lazy="dynamic"))


# # Define the UserRoles association table
class UserRole(db.Model):
__tablename__ = 'UserRoles'
id = db.Column(db.Integer(), primary_key=True)
user_id = db.Column(db.Integer(), db.ForeignKey('Users.id', ondelete='CASCADE'))
role_id = db.Column(db.Integer(), db.ForeignKey('Roles.id', ondelete='CASCADE'))


In my views.py:



from flask_user import current_user

@app.route("/TestUserRole")
def user_role():
role = current_user.role
return str(role)


On the browser, it shows me the returned result:



SELECT [Roles].id AS [Roles_id], [Roles].name AS [Roles_name] FROM [Roles], [UserRoles] WHERE [UserRoles].user_id = ? AND [Roles].id = [UserRoles].role_id


Is there a possible way to show the role name of the current user, something like the following (bad example, though, since it will pop out error)?



role = current_user.role.name









share|improve this question














I have 3 tables for user-role many-to-many relationship in Flask. How should I get the role name from the current user?



In my models.py



class User(db.Model, UserMixin):
__tablename__ = "Users"
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(100), nullable=False, unique=True)
password = db.Column(db.String(255), nullable=False, server_default='')

# Define the relationship to Role via UserRoles
roles = db.relationship('Role', secondary='UserRoles', backref=db.backref("user", lazy="dynamic"))


# Define the Role data-model
class Role(db.Model):
__tablename__ = 'Roles'
id = db.Column(db.Integer(), primary_key=True)
name = db.Column(db.String(50), unique=True)
users = db.relationship('User', secondary='UserRoles', backref=db.backref("role", lazy="dynamic"))


# # Define the UserRoles association table
class UserRole(db.Model):
__tablename__ = 'UserRoles'
id = db.Column(db.Integer(), primary_key=True)
user_id = db.Column(db.Integer(), db.ForeignKey('Users.id', ondelete='CASCADE'))
role_id = db.Column(db.Integer(), db.ForeignKey('Roles.id', ondelete='CASCADE'))


In my views.py:



from flask_user import current_user

@app.route("/TestUserRole")
def user_role():
role = current_user.role
return str(role)


On the browser, it shows me the returned result:



SELECT [Roles].id AS [Roles_id], [Roles].name AS [Roles_name] FROM [Roles], [UserRoles] WHERE [UserRoles].user_id = ? AND [Roles].id = [UserRoles].role_id


Is there a possible way to show the role name of the current user, something like the following (bad example, though, since it will pop out error)?



role = current_user.role.name






python flask flask-sqlalchemy flask-login






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 9 at 3:35









D EmmaD Emma

62




62












  • Try user_roles = [role.name for role in current_user.roles]

    – tntxtnt
    Mar 9 at 4:27











  • Can you not do something like Role.query.get(**current users role id**) and add this as an extra column? I'm new too :)

    – Andrew Allen
    Mar 11 at 16:19

















  • Try user_roles = [role.name for role in current_user.roles]

    – tntxtnt
    Mar 9 at 4:27











  • Can you not do something like Role.query.get(**current users role id**) and add this as an extra column? I'm new too :)

    – Andrew Allen
    Mar 11 at 16:19
















Try user_roles = [role.name for role in current_user.roles]

– tntxtnt
Mar 9 at 4:27





Try user_roles = [role.name for role in current_user.roles]

– tntxtnt
Mar 9 at 4:27













Can you not do something like Role.query.get(**current users role id**) and add this as an extra column? I'm new too :)

– Andrew Allen
Mar 11 at 16:19





Can you not do something like Role.query.get(**current users role id**) and add this as an extra column? I'm new too :)

– Andrew Allen
Mar 11 at 16:19












0






active

oldest

votes












Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073739%2fin-python-flask-how-to-get-the-role-name-by-the-current-logged-in-user%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55073739%2fin-python-flask-how-to-get-the-role-name-by-the-current-logged-in-user%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived