[英]How to retrieve an object or a particular field from context object in view?
我的代碼如下:dept_id 和 desi_id 是entries3 views.py 的字段
def claim(request):
entries = Child_Info.objects.all()
entries2 = Employee.objects.filter(emp_name='Suresh Babubhai Vekariya')
entries3 = Emp_Department.objects.filter(emp_no=12345)
for a in entries3:
dept_id = a.dept_id;
desi_id = a.desi_id
print dept_id
ctx = { 'entries' : entries ,'entries2' : entries2 }
return render_to_response('myapp/claim.html', ctx)
這是3個模型:
class Department(models.Model):
dept_id = models.CharField(primary_key=True,max_length=20,unique=True)
dept_name = models.CharField(max_length=20)
dept_add = models.CharField(max_length=30)
def _str_(self):
return self.dept_id
class Emp_Department(models.Model):
emp_no = models.ForeignKey(Employee)
dept_id = models.ForeignKey(Department)
emp_type = models.CharField(max_length=30)
desi_id = models.CharField(max_length=30)
join_date = models.DateField()
retire_date = models.DateField()
def _str_(self):
return "%s,%s,%s" %(self.desi_id,self.dept_id,self.emp_no)
class Designation(models.Model):
desi_id = models.CharField(primary_key=True,max_length=30)
dept_id = models.ForeignKey(Department)
title = models.CharField(max_length=20)
def _str_(self):
return "%s,%s,%s" %(self.desi_id,self.dept_id,self.title)
我想從指定和部門名稱中獲取標題
您已經根據特定字段過濾了結果。
entries3 = Emp_Department.objects.filter(emp_no=12345)
如果您嘗試詢問如何在模板中顯示特定字段,然后在您的claim.html文件中,請嘗試以下操作:
{{entries2.0.emp_name}}
這將顯示第一個員工的 emp_name。 同樣,您可以將 emp_name 替換為要在模板中顯示的任何字段。
我希望這是你想問的,因為這個問題不太清楚。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.