Home > database >  relation " " does not exist in Django
relation " " does not exist in Django

Time:02-25

I have a doubt what does it mean:

relation "clientes" does not exist
LINE 1: select nombre from Clientes where joindate between "2022-02-...

It seems strange to me; or that this error appears since my model is called Customers; I just did the migrations on heroku, but I get this error

In my views I have this:

def list_clientes(request):


    if request.method == 'POST':
        fromdate=request.POST.get('fromdate')
        todate = request.POST.get('todate')
        searchresult=Clientes.objects.raw('select nombre from Clientes where joindate between "' fromdate '" and "' todate '"')
        return render(request,'Clientes/clientes-list.html',{'clientes':searchresult})

    else:
        displaydata = Clientes.objects.all()
    return render(request, 'Clientes/clientes-list.html', {'clientes': displaydata})



models.py


class Clientes(models.Model):
    tipo = models.CharField(max_length=200)
    nombre = models.CharField(max_length=200)
    fecha_registro = models.DateTimeField(default=datetime.now)

    def __str__(self):
        return f'{self.nombre}'


settings.py


DATABASES = {
    'default': {
        'ENGINE':'django.db.backends.postgresql_psycopg2',
        'NAME':'sistemacarros_db',
        'USER':'postgres',
        'PASSWORD':'1234',
        'HOST':'localhost',
        'PORT':'5432',
        'OPTIONS':{
            'init_command':"SET sql_mode='STRICT_TRANS_TABLES'",
        }
    }
}

CodePudding user response:

Please use the Django ORM:

def list_clientes(request):
    if request.method == 'POST':
        fromdate=request.POST.get('fromdate')
        todate = request.POST.get('todate')
        searchresult = Clientes.objects.filter(fecha_registro__range=(fromdate, todate))
    else:
        searchresult = Clientes.objects.all()
    return render(request,'Clientes/clientes-list.html',{'clientes':searchresult})

Note: Django's DateTimeField [Django-doc] has a auto_now_add=… parameter [Django-doc] to work with timestamps. This will automatically assign the current datetime when creating the object, and mark it as non-editable (editable=False), such that it does not appear in ModelForms by default.

  • Related