我正在努力让这个观点发挥作用。在代码中我添加了一个注释,指出了问题所在。基本上我不能为我的生活得到与EmployeeProfile.team相关的TeamsWeeklyMasterSchedule对象
模型
class Team(models.Model):
name = models.CharField(max_length=10)
def __str__(self):
"""Return a string representation of the model."""
return self.name
class TeamsWeeklyMasterSchedule(models.Model):
"""Hours Available For That Day"""
team = models.ForeignKey(Team, on_delete=models.CASCADE)
class EmloyeeProfile(models.Model):
owner = models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE, )
team = models.ForeignKey(Team, on_delete=models.CASCADE,)
查看
@login_required
def employee(request):
"""The home page"""
profile = EmployeeProfile.objects.filter(owner=request.user)
# I Cannot make this get() work!
teams_weekly_master_schedule = TeamsWeeklyMasterSchedule.objects.get()
context = {
'profile': profile,
'teams_weekly_master_schedule': teams_weekly_master_schedule,
}
return render(request, 'portal/employee.html', context)
我尝试过什么
teams_weekly_master_schedule = TeamsWeeklyMasterSchedule.objects.get(team=profile.team)
teams_weekly_master_schedule = TeamsWeeklyMasterSchedule.objects.get(team=request.user.team)
答案 0 :(得分:2)
我能够复制这个问题,我解决了这个问题:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
第一个问题是在第3行,您使用过滤器来检索一个EmployeeProfile对象。使用过滤器返回一个查询集,该查询集不能作为对象访问。
当您使用GET时,您可以使用profile.team返回您想要的团队。