I'm trying to create a Model in a way where I can which users each user has created. So far, I have a User Model:
class User(db.Model):
__tablename__ = "usersTable"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String(128), nullable=False)
password = db.Column(db.String(255), nullable=False)
active = db.Column(db.Boolean(), default=True, nullable=False)
created_date = db.Column(db.DateTime, default=func.now(), nullable=False)
Here is my create User Function:
const createUser = (values) => {
const data = { refresh: window.localStorage.getItem('refreshToken') };
axios
.post(`${SERVICE_URL}/api/v1/auth/token/refresh`, data)
.then((res) => {
const config = { headers: {'Authorization': `Bearer ${res.data.access}`}};
axios
.post(`${SERVICE_URL}/api/v1/users`, values, config)
.then((res) => {
setRedirect(true);
})
.catch((err) => {
setRedirect(false);
setType('danger');
setText('Unable to create user. Does this username already exist?');
});
})
.catch((err) => {
setRedirect(true);
});
};
How can I modify this model so that each User can keep track of the users they've created?
I've looked into created a pickled attribute however I think that would be an over-complication for this problem
CodePudding user response:
To make sure I get it right, you're trying to create a model of a user of an application in which you wish to keep track of which user created which user ??
You mentioned each user keeps track of the users they created.
So for that we can add an extra field which stores which user created the new user. This field could be a string field and the model would be as follows:
class User(db.Model):
__tablename__ = "usersTable"
id = db.Column(db.Integer, primary_key=True, autoincrement=True)
username = db.Column(db.String(128), nullable=False, unique=True) # making username unique.
password = db.Column(db.String(255), nullable=False)
active = db.Column(db.Boolean(), default=True, nullable=False)
created_date = db.Column(db.DateTime, default=func.now(), nullable=False)
CreatedBy = db.Column(db.String(128), default="", nullable=False) # TO STORE THE USERNAME OF THE USER WHO CREATED THIS USER.
usersCreated = db.Column(db.ARRAY(db.String(128)), default=[])
Now let's say there's a page in your application that's shows the list of users. On clicking of the individual users buttons, we would be redirected to a page showing information about the user. So to get a list of all users created by the user that we have clicked on can be done using the following statement.
usersCreatedByThisPerson = User.query.filter_by(CreatedBy = <name of the person who was clicked on>).all()
This returns a list of instance of the Users model.
I don't know if I answered your query. A lot of things had to be assumed while answering.
Let me know if I didn't answer your query. Thanks.
Okay, so editing the answer based on your comment.
What we will do is in the model, we will add another column to store the usernames of all the users that is created by this user.
How will we do it ?
In the POST Api, we will send this, username, password, CreatedBy and then we will update the users table accordingly.
Let's say the json is:
{
"username": usernameLatest,
"password": password,
"CreatedBy": usernameOld
}
Now in the post API, we will append usernameLatest in the usersCreated column in the row that has username=usernameOld.
So in the post API, we will do something like this:
oldUserInstance = Users.query.filter_by(username=usernameOld).first() # since username is unique, we just want the first one.
list(oldUserInstance.usersCreated).append(usernameLatest)
In this way, we can keep updating all the users created by the user that is creating the current user.
Let me know if it was clear.
Thanks.