Reputation: 1
This is my first coding python/flask/sqlalchemy project. I want to create a page where a HR can assign a selected project to a selected employee. One employee can work with many projects. Many employees can work in one project (many to many relation).
My problem, I couldn't get the selected project for the selected employee and fill out the association table. I tried to search but couldn't find relevant topic. Thank's for response!
Code for database part
my_db.py:
Employee_Task = db.Table('employee_task',
db.Column('employee_id', db.Integer, db.ForeignKey('employee.employee_id')),
db.Column('task_id', db.Integer, db.ForeignKey('task.task_id'))
)
class Employee(db.Model):
__tablename__ = 'employee'
employee_id = db.Column(db.Integer, primary_key=True)
employee_name = db.Column(db.String(100), nullable=True)
rel_task = db.relationship("Task", secondary=Employee_Task)
# class Task
class Task(db.Model):
__tablename__ = 'task'
task_id = db.Column(db.Integer, primary_key=True)
task_name = db.Column(db.String(100), nullable=True)
def __repr__(self):
return "<Task %r>" % self.name
Part of main code, As you can see it adds new row with same employee_id and task_id, but I want to add new row with choosen employee_id with choosen task_id
Let's say when HR assign to 5th employee 8th project, I want to see in one row of association table:
5 in employee_id column
8 in project_id column
main.py:
@app.route("/employee", methods=['POST', 'GET'])
def employee():
tasks_all = Task.query.all()
workers_all = Employee.query.all()
if request.method == 'POST'
idS = str(int(request.form.get("Assigned_Task")))
EmployeeID = idS[0] #this is choosen employee, I want add this employee to association table
TaskID = idS[-1] #this is choosen project, I want add this project to association table
e = Employee()
t = Task()
e.rel_task.append(t)
db.session.add(e)
db.session.commit()
return render_template('my.html', tasks_all=tasks_all, workers_all=workers_all, form=search,
title='List of Employee')
part of the html page (as you can see it's table, for given list of employee HR can choose a project from drop-down menu and assign it)
<table class="table table-hover">
<thread>
<tr class="table-info font-italic">
<th>Employees</th>
<th>Projects</th>
</tr>
</thread>
<tbody>
{% for worker in workers_all %}
<tr>
<td rowspan="1">{{ worker.employee_name }}</td>
<td rowspan>
<form action="/employee" method="POST">
<div class="input-group">
<select class="form-control" name="Assigned_Task">
{% for task in tasks_all %}
<option value="{{ worker.employee_id }} , {{ task.task_id }}">
{{ task.task_name }}
</option>
{% endfor %}
</select>
<div class="input-group-append">
<button type=submit value="" class="btn btn-sm btn-info">
<i class="fas fa-user-check"></i>
<a class="mr-1">assign</a>
</button>
</div>
</div>
</form>
</td>
</tr>
{% endfor %}
</tbody>
</table>
Upvotes: 0
Views: 956
Reputation: 1
main.py:
@app.route("/employee", methods=['POST', 'GET'])
def employee():
tasks_all = Task.query.all()
workers_all = Employee.query.all()
if request.method == 'POST'
idS = str(int(request.form.get("Assigned_Task")))
EmployeeID = idS[0]
TaskID = idS[-1]
e_qry = db.session.query(Employee).filter(Employee.employee_id.contains(EmployeeID))
t_qry = db.session.query(Task).filter(Task.task_id.contains(TaskID))
e = e_qry.first()
t = t_qry.first()
e.rel_task.append(t)
db.session.add(e)
db.session.commit()
return render_template('my.html', tasks_all=tasks_all, workers_all=workers_all, form=search, title='List of Employee')
Upvotes: 0
Reputation: 1438
I guess you're sending an string representation of a tuple. You must convert it. You can do it by importing the python built-in ast
library.
import ast
@app.route("/employee", methods=['POST', 'GET'])
def employee():
tasks_all = Task.query.all()
workers_all = Employee.query.all()
if request.method == 'POST'
idS = ast.literal_eval(request.form.get("Assigned_Task"))
Employee.id, Task.id = idS
e = Employee()
t = Task()
e.rel_task.append(t)
db.session.add(e)
db.session.commit()
return render_template('my.html', tasks_all=tasks_all, workers_all=workers_all, form=search,
title='List of Employee')
Upvotes: 1