有些用户是“编码员”,他们有“作业”。
class Coder(models.Model):
"""Every user can be a coder. Coders are assigned to Assignments"""
user = models.OneToOneField(User, related_name='coder')
def __unicode__(self):
return self.user.username
class Assignment(models.Model):
"""An assignment covers a range of years,
has multiple coders and one specific task"""
title = models.CharField(blank=True, max_length=100)
start_date = models.DateField(default=datetime.datetime.today)
end_date = models.DateField(default=datetime.datetime.today)
country = models.ForeignKey(Country, default=1, related_name='assignments')
coders = models.ManyToManyField(User, related_name='assignments')
screening = models.BooleanField(default=False)
classifying = models.BooleanField(default=False)
def __unicode__(self):
return self.country
如果编码员已登录,如何向他们显示已提供给他们的所有作业? 以下视图不起作用,它给我“'Coder'对象没有属性'赋值'”。
def user_view(request):
if request.user.is_authenticated():
assignments = request.user.coder.assignments.all()
else:
pass
return render(request, 'account.html')
为什么这不起作用? 我怎样才能使它发挥作用?
答案 0 :(得分:1)
使用request.user.coder.assignments.all()
。
您未在通话中传递作业