在我的项目中,我的操作很复杂,因此当我选择部门时,我保存该项目的ID并绑定到表以显示插入的记录,因此当我绑定部门时,我想绑定该部门的名称而不是该ID 。请帮助我。
Model.py
list(dictionary.items())
forms.py
class Department(models.Model):
ACTIVE = 1
INACTIVE = 2
DELETED = 3
STATUS_CHOICES = (
(ACTIVE, 'active'),
(INACTIVE, 'inactive'),
(DELETED, 'deleted'),
)
department_id = models.AutoField(primary_key=True)
department_name = models.CharField(max_length=30, null=False)
created_on = models.DateTimeField(auto_now_add=True, null=False)
created_by = models.CharField(max_length=100, null=False)
modified_on = models.DateTimeField(auto_now_add=True)
modified_by = models.CharField(max_length=100)
status = models.CharField(max_length=10, null=False, choices=STATUS_CHOICES)
objects = UserManager()
class Meta:
managed = True
db_table = "ht_department"
def __str__(self):
return self.department_id
html
class EmpForm(ModelForm):
class Meta:
model = Employee
fields = ["employee_id", "Name", "designation", "department_id", "manager_id",
"date_of_joining","date_of_birth", "location_id", "email", "contact_number",
"password", "created_by", "modified_by", "status", "user_type"]
class dept(ModelForm):
class Meta:
model = Department
fields = ["department_id", "department_name", "created_by", "modified_by", "status"]
class EmpLoc(ModelForm):
class Meta:
model = Location
fields = ["location_id", "location_name", "created_by", "modified_by", "status"]
这是我更新的EmployeeModel代码:
<tbody id="myTable">
{% for employee in employees %}
<tr>
<td>{{ employee.employee_id}}</td>
<td>{{ employee.Name}}</td>
<td>{{ employee.designation}}</td>
<td>{{ employee.department_id}}</td>
<td>{{ employee.manager_id}}</td>
<td>{{ employee.location_id}}</td>
<td>
<a href="#editEmployeeModal" class="edit" data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Edit"></i></a>
<a href="#deleteEmployeeModal" class="delete" data-toggle="modal"><i class="material-icons" data-toggle="tooltip" title="Delete"></i></a>
</td>
</tr>
{% endfor %}
</tbody>
答案 0 :(得分:0)
您应该将员工中的department_id字段更新为外键,以便获取部门名称,因此它将为:
class Employee(models.Model):
department = models.ForeignKey(Department)
然后在模板中获取部门名称
{{employee.department.department_name}}