'Django Many-to-Many query not returning related records
so this is my job records model that has a many to many relationship with employees model, but when I try to get the job records using {{ employee.jobs }} in my template it returns nothing
I can successfully get the employee info, but not the jobs, it's as if they're not related (which they are I checked from database)
Jobs Model
class Jobs(models.Model):
employee_id = models.ManyToManyField(Employee)
department_id = models.ForeignKey(Departments,null=True,on_delete=models.SET_NULL)
job_title_id = models.ManyToManyField(Job_titles)
start_date = models.DateField(null=True)
end_date = models.DateField(null=True)
salary = models.IntegerField(null=True)
created_at = models.DateTimeField(auto_now_add=True)
modified_at = models.DateTimeField(auto_now=True)
primary = models.BooleanField(null=True)
hire_date = models.DateField(null=True,blank=True)
pay_schedule = models.ForeignKey(Pay_schedule,max_length=100,null=True,blank=True,on_delete=models.SET_NULL)
pay_type = models.ForeignKey(Pay_type,max_length=100,null=True,blank=True,on_delete=models.SET_NULL)
division = models.ForeignKey(Divisions,max_length=100,null=True,blank=True,on_delete=models.SET_NULL)
My View
def JobView(request,username):
if request.user.is_authenticated:
try:
employee = Employee.objects.get(user__username=username)
return render(request,'job.html',context={'employee':employee})
except:
pass
also , when I directly try to filter job records using jobs = Jobs.objects.filter(employee__id=1) I get this :
The view Employees.views.JobView didn't return an HttpResponse object. It returned None instead.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
