我正在尝试使用从一个应用程序到另一个应用程序的模型,但Django没有识别它的名称并给出以下错误:
local variable 'user_status' referenced before assignment
这是我的模特:
class user_status(models.Model):
name = models.CharField(max_length=500)
code = models.IntegerField()
def __unicode__(self):
return u'%s' % self.name
这是我的观点:
def verifica_pgto(request):
if request.user.is_authenticated():
user = request.user
date_joined = user.date_joined.replace(tzinfo=None)
dif = datetime.now() - date_joined
try:
user_details = UserDetails.objects.get(user=user)
if user_details.status == 'ativo':
params = {'user_status':'ativo'}
else:
params = {'user_status':'inativo'}
except:
# cria user details
if dif.total_seconds() > 7:
status = 'inativo'
user_status = user_status.objects.get(name = 'inativo')
else:
status = 'ativo'
user_status = user_status.objects.get(name = 'inativo')
d = UserDetails(
user = user,
created_at = datetime.now(),
status = user_status,
end_access = dt.timedelta(days=7)
)
d.save()
if user_status == '':
params = {'user_status':'ativo'}
else:
params = {'user_status':'inativo'}
return params
这就是我将模型导入views.py
的方法from payments.models import *
我也尝试过这种方式:
from payments.models import *
from payments.models import user_status
答案 0 :(得分:1)
尽量不要使用与class相同的实例名称。试试这个:
from django.shortcuts import get_object_or_404
from payments.models import user_status
def verifica_pgto(request):
if ....
us = get_object_or_404(user_status)
......
us.objects.get(name = 'inativo')
....