【发布时间】:2021-01-20 00:00:15
【问题描述】:
假设 get 方法在 post 方法之前至少被调用一次。(这不是我的实际代码,如果有任何愚蠢的错误,不要考虑它,我的问题是我可以实例化并使用下面的静态变量吗)
class McqView(View):
course=None
all_questions=None
start_time=None
def get(self,request,someid):
if not McqView.course:
McqView.course = Course.objects.get(id=someid)
if not McqView.all_questions:
McqView.all_questions = Question.objects.filter(course=McqExamView.course)
if not McqView.start_time:
McqView.start_time = datetime.now()
#using the static variables somewhere here
def post(self,request):
#using the same static variables somewhere here
我想过使用这两个作为成员变量,但这是不可能的。所以我想到了将两者用作静态变量。使用这种方法会不会出现问题?喜欢 1.静态变量变为None 2.静态变量不止一次被实例化。 如果发生第二种情况,start_time变量的值就会出错。
请告诉我这是否是错误的方法,如果是错误的,请告诉我另一种方法。
【问题讨论】:
标签: python django django-views static-variables