我在Django中为模型管理器提供了两种自定义方法。其中一个有效。我最近添加了另一个和Django(和python)的行为,就像它不存在一样。这是模型的相关部分:
class FigureServerManager(models.Manager):
#This method takes as input a user and grabs a figure that is not marked complete for which that user has not already submitted a result
def serve_to_user(self,user):
not_complete=super(FigureServerManager, self).get_query_set().filter(complete=0)
for Figure in not_complete:
checkifresult=User.objects.get(pk=user).result_set.all().filter(figure=Figure.id)
if not checkifresult:
return Figure
#This is a copy of the above method that I want to change to do something else, but I can't even get it to show up yet
def serve_training_task(self, user):
with_correct_ans=super(FigureServerManager, self).get_query_set().filter(complete=0)
for Figure in with_correct_ans:
checkifresult=User.objects.get(pk=user).result_set.all().filter(figure=Figure.id)
if not checkifresult:
return Figure
class Figure(models.Model):
doi=models.CharField(max_length=20)
url=models.CharField(max_length=200)
image=models.ImageField(upload_to='classify')
complete=models.BooleanField()
#include the default manager
objects=models.Manager()
#add the extra one for serving figures
serve_objects=FigureServerManager()
我在网站上遇到错误(运行Django开发服务器),如下所示:
'FigureServerManager' object has no attribute 'serve_training_task'
如果我在python中运行dir(FigureServerManager),则不会出现serve_training_task方法,但会出现serve_to_user方法。为什么serve_training_task不起作用?