Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用py字典获取表中ID的名称?_Python_Django_Dictionary - Fatal编程技术网

Python 如何使用py字典获取表中ID的名称?

Python 如何使用py字典获取表中ID的名称?,python,django,dictionary,Python,Django,Dictionary,我在数据库的下拉列表中有所选部门的id,我想将该id的名称绑定到表中,这样就可以使用py字典。。怎么做 html views.py def managemp(request): employees = Employee.objects.all() department = Department.objects.all() location = Location.objects.all() return render(request, "manageemp.htm

我在数据库的下拉列表中有所选部门的id,我想将该id的名称绑定到表中,这样就可以使用py字典。。怎么做

html

views.py

def managemp(request):

    employees = Employee.objects.all()
    department = Department.objects.all()
    location = Location.objects.all()
    return render(request, "manageemp.html", {'department': department, 'employees': employees, 'location': location})

添加视图和模型代码。部门id应该是外键

  class Employee(models.Model):
       ACTIVE = 1
       INACTIVE = 2
       DELETED = 3

       STATUS_CHOICES = (
           (ACTIVE, 'active'),
           (INACTIVE, 'inactive'),
           (DELETED, 'deleted'),
       )
       USER_TYPE = (
           (1, 'user'),
           (2, 'hr'),
       )
       employee_id = models.CharField(max_length=15, primary_key=True)
       Name = models.CharField(max_length=100, null=False)
       designation = models.CharField(max_length=30, null=False)
       department_id = models.IntegerField(null=False)
       manager_id = models.CharField(max_length=15, null=False)
       date_of_joining = models.DateField(null=False)
       date_of_birth = models.DateField(null=False)
       location_id = models.IntegerField(null=False)
       email = models.EmailField(max_length=100, unique=True, null=False)
       contact_number = models.CharField(max_length=10 , unique=True, null=False)
       password = models.CharField(max_length=100, null=False)
       created_on = models.DateTimeField(auto_now_add=True)
       created_by = models.CharField(max_length=100, null=False)
       modified_on = models.DateTimeField(auto_now_add=True)
       modified_by = models.CharField(max_length=100, null=True)
       status = models.CharField(max_length=10, null=False, choices=STATUS_CHOICES)
       user_type = models.CharField(max_length=10, choices=USER_TYPE)

       objects = UserManager()

       USERNAME_FIELD = "email"
       REQUIRED_FIELDS = ["Name"]

       is_anonymous = "FALSE"
       is_authenticated = "TRUE"

       thisdict = {
           "1": "Investment",
           "2": "Health",
           "3": "Motor",
           "4": "Term"
       }

       class Meta:
           managed = True
           db_table = "ht_employee"


   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

def managemp(request):

    employees = Employee.objects.all()
    department = Department.objects.all()
    location = Location.objects.all()
    return render(request, "manageemp.html", {'department': department, 'employees': employees, 'location': location})