0

I'm using django framework with postgresql. I have virtual environment. My local machine Mac , server is on Debian 10.

$ pip freeze
amqp==5.0.6
asgiref==3.4.1
billiard==3.6.4.0
celery==5.1.2
certifi==2021.5.30
chardet==4.0.0
click==7.1.2
click-didyoumean==0.0.3
click-plugins==1.1.1
click-repl==0.2.0
Django==3.2.5
django-celery-beat==2.2.1
django-celery-results==2.2.0
django-timezone-field==4.1.2
greenlet==1.1.0
idna==2.10
kombu==5.1.0
lxml==4.6.3
multitasking==0.0.9
numpy==1.21.0
pandas==1.3.0
plotly==5.1.0
prompt-toolkit==3.0.19
psycopg2==2.9.1
psycopg2-binary==2.9.1
python-crontab==2.5.1
python-dateutil==2.8.1
pytz==2021.1
requests==2.25.1
six==1.16.0
SQLAlchemy==1.4.20
sqlparse==0.4.1
tenacity==7.0.0
urllib3==1.26.6
vine==5.0.0
wcwidth==0.2.5
yfinance==0.1.60

models.py

from django.db import models
from django.utils import timezone

class Ticker(models.Model):
    symbol = models.CharField(max_length=12)
    description = models.CharField(max_length=100, blank=True, default='')
    avg_volume = models.DecimalField(max_digits=20, decimal_places=6)
    last_close_price = models.DecimalField(max_digits=20, decimal_places=6, default=0)
    last_update_date = models.DateField(default=timezone.now)

    def __str__(self):
        return self.symbol

class TickerData(models.Model):
    date = models.DateField(default=timezone.now)
    open_price = models.DecimalField(max_digits=20, decimal_places=6)
    high_price = models.DecimalField(max_digits=20, decimal_places=6)
    low_price = models.DecimalField(max_digits=20, decimal_places=6)
    close_price = models.DecimalField(max_digits=20, decimal_places=6)
    adj_close = models.DecimalField(max_digits=20, decimal_places=6)
    volume = models.IntegerField()
    symbol = models.ForeignKey(Ticker, on_delete=models.CASCADE, related_name='infoset')

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

views.py

from datetime import date, datetime, timedelta
from django.db import connection
from django.shortcuts import render, get_object_or_404

from .models import Ticker, TickerData

def ticker_data_view(request, pk):
    # dates
    enddate = date.today()
    startdatedays = enddate - timedelta(days=180)

    ticker = Ticker.objects.get(pk=pk)
    ticker_info = TickerData.objects.filter(symbol=ticker).filter(date__gte=startdatedays)
    
    context = {
        'TickerData': ticker_info,
    }


    return render(request, 'tickerdata_list.html', context)

When I open page with info

DatabaseError at /ticker/2 Execution failed on sql 'SELECT "webapp_tickerdata"."id", "webapp_tickerdata"."date", "webapp_tickerdata"."open_price", "webapp_tickerdata"."high_price", "webapp_tickerdata"."low_price", "webapp_tickerdata"."close_price", "webapp_tickerdata"."adj_close", "webapp_tickerdata"."volume", "webapp_tickerdata"."symbol_id" FROM "webapp_tickerdata" WHERE ("webapp_tickerdata"."symbol_id" = 2 AND "webapp_tickerdata"."date" >= 2021-01-09)': operator does not exist: date >= integer LINE 1: ...a"."symbol_id" = 2 AND "webapp_tickerdata"."date" >= 2021-01...
                                                             ^ HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.

Can somebody help please.

UPDATE

I think my postgres waiting for date in quotes, like:

AND "webapp_tickerdata"."date" >= '2021-01-09'

I'd tried it directly , just select with such params and it works. Maybe I need to change settings in postgress?

5
  • Try this: startdatedays = (enddate - timedelta(days=180)). It may seem dumb but i had a similar instance not long ago where this worked Commented Jul 8, 2021 at 8:27
  • @Beikeni Unfortunately it didn't help =((( Commented Jul 8, 2021 at 8:40
  • The problem is here operator does not exist: date >= integer . You are comparing a date to an integer. Im not sure why it takes a date as an integer but maybe try using a datetime element instead? Like datetime.today() instead of date.today(). Just to see if the problem is with date.today(). If the error persists then you'll know its something else. Commented Jul 8, 2021 at 8:52
  • @Beikeni I'd tried it. The same result =( Commented Jul 8, 2021 at 9:35
  • @Beikeni I think my postgres waiting for date in quotes, like: AND "webapp_tickerdata"."date" >= '2021-01-09' I'd tried it directly , just select with such params and it works. Maybe I need to change settings in postgress? Commented Jul 13, 2021 at 10:43

1 Answer 1

1

I think the problem is in this line

date = models.DateField(default=timezone.now)

You use date field but timezone.now give you a date and time Change this like that

import datetime 
date = models.DateField(default=datetime.date.today)

Or maybe this is work aswell

date = models.DateField(default=timezone.now.date)
Sign up to request clarification or add additional context in comments.

6 Comments

I'd tried - default=datetime.date.today. It didn't help. default=timezone.now.date - get an error 'function' object has no attribute 'date'
Whats the problem with first one?
DatabaseError at /ticker/2 Execution failed on sql 'SELECT "webapp_tickerdata"."id", "webapp_tickerdata"."date", ...."webapp_tickerdata"."adj_close", "webapp_tickerdata"."volume", "webapp_tickerdata"."symbol_id" FROM "webapp_tickerdata" WHERE ("webapp_tickerdata"."symbol_id" = 2 AND "webapp_tickerdata"."date" >= 2021-01-09)': operator does not exist: date >= integer LINE 1: ...a"."symbol_id" = 2 AND "webapp_tickerdata"."date" >= 2021-01... HINT: No operator matches the given name and argument types. You might need to add explicit type casts.
You run makemigrations and migrate after changes?
Yes, i run python manage.py makemigrations and then migrate
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.