Search code examples
djangodjango-modelsdjango-viewsdjango-users

How to fix 'CustomUser' object has no attribute 'get'


I am trying to get some data from the database to display on the view, but I shows me an error that I have code that is not even there.

I have the model 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)

And I have a view with the following method:

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)

And my urls patterns from that model is:

urlpatterns = [
    path('cliente/<int:id>', CustomCliente, name='relatorio'),
]

What happens is that when I try to access the following url 127.0.0.1:8000/cliente/2, I get the error: 'CustomCliente' object has no attribute 'get. Even though I don't even have anywhere in my code calling CustomCliente.get. I have tried shutting down the server and trying again, rewrited the code, but it doesn't seem to work.


Solution

  • You have used your model in urls.py instead of your view.

    It should be:

    path('cliente/<int:id>', mostrar_relatorio, name='relatorio'),