如何修复 'CustomUser' 对象没有属性 'get'
How to fix 'CustomUser' object has no attribute 'get'
我试图从数据库中获取一些数据以显示在视图上,但我显示了一个错误,提示我的代码甚至不存在。
我有模型 CustomCliente:
class CustomCliente(AbstractUser):
email = models.EmailField(unique=True, null=False, blank=False)
full_name = models.CharField(max_length=120, null=False, blank=False)
password = models.CharField(max_length=40, null=False, blank=False)
username = models.CharField(max_length=110, unique=True, null=False, blank=False)
而且我有一个视图,方法如下:
def mostrar_relatorio(request, id):
aluguel = Aluguel.objects.get(cliente=CustomCliente.objects.get(id=id))
if aluguel is not None :
context = {'aluguel': aluguel}
template_name = 'relatorio.html'
else:
raise Http404
return render(request, template_name, context)
我的 url 模型的模式是:
urlpatterns = [
path('cliente/<int:id>', CustomCliente, name='relatorio'),
]
当我尝试访问以下 url 127.0.0.1:8000/cliente/2 时,我收到错误消息:
'CustomCliente' 对象没有属性 'get.即使我的代码中甚至没有任何地方调用 CustomCliente.get。
我试过关掉服务器再试,重写了代码,但是好像不行。
您在 urls.py 中使用了您的模型,而不是您的视图。
应该是:
path('cliente/<int:id>', mostrar_relatorio, name='relatorio'),
我试图从数据库中获取一些数据以显示在视图上,但我显示了一个错误,提示我的代码甚至不存在。
我有模型 CustomCliente:
class CustomCliente(AbstractUser):
email = models.EmailField(unique=True, null=False, blank=False)
full_name = models.CharField(max_length=120, null=False, blank=False)
password = models.CharField(max_length=40, null=False, blank=False)
username = models.CharField(max_length=110, unique=True, null=False, blank=False)
而且我有一个视图,方法如下:
def mostrar_relatorio(request, id):
aluguel = Aluguel.objects.get(cliente=CustomCliente.objects.get(id=id))
if aluguel is not None :
context = {'aluguel': aluguel}
template_name = 'relatorio.html'
else:
raise Http404
return render(request, template_name, context)
我的 url 模型的模式是:
urlpatterns = [
path('cliente/<int:id>', CustomCliente, name='relatorio'),
]
当我尝试访问以下 url 127.0.0.1:8000/cliente/2 时,我收到错误消息: 'CustomCliente' 对象没有属性 'get.即使我的代码中甚至没有任何地方调用 CustomCliente.get。 我试过关掉服务器再试,重写了代码,但是好像不行。
您在 urls.py 中使用了您的模型,而不是您的视图。
应该是:
path('cliente/<int:id>', mostrar_relatorio, name='relatorio'),