一些用户是有“任务”的“编码员”。
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' 对象没有属性'assignments'”。
def user_view(request):
if request.user.is_authenticated():
assignments = request.user.coder.assignments.all()
else:
pass
return render(request, 'account.html')
为什么这不起作用?我怎样才能让它工作?