Djongo.exceptions.SQLDecodeError trying to create model object

I have the following stripped down view that is giving strange error:

def main_dividends_results(request, ticker):
    stock, created = StockInfo.objects.get_or_create(ticker=ticker)
    return HttpResponse("worked")

I get error

 File "/root/stocks_backend/stocks-env/lib/python3.8/site-packages/djongo/sql2mongo/", line 368, in _fill_values
    raise SQLDecodeError

    Keyword: None
    Sub SQL: None
    FAILED SQL: ('INSERT INTO "dividends_info_stockinfo" ("ticker", "current_price", "name", "summary", "sector", "dividends", "earnings", "last_updated_time") VALUES (%(0)s, %(1)s, %(2)s, %(3)s, %(4)s, %(5)s, %(6)s, %(7)s)',)
    Params: (['AA', None, None, None, None, None, None, datetime.datetime(2022, 9, 13, 19, 16, 21, 484492)],)
    Version: 1.3.6

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/root/stocks_backend/stocks-env/lib/python3.8/site-packages/django/db/backends/", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/root/stocks_backend/stocks-env/lib/python3.8/site-packages/djongo/", line 59, in execute
    raise db_exe from e

I don't understand the issue because all the model fields allow null:

from djongo import models

class Dividend(models.Model):
    date = models.DateField()
    amount = models.FloatField()

    class Meta:
        abstract = True

class Earning(models.Model):
    date = models.DateField()
    expected = models.CharField(max_length=100)
    actual = models.CharField(max_length=100)
    surprise = models.CharField(max_length=10)

    class Meta:
        abstract = True

class StockInfo(models.Model):
    id = models.BigAutoField(primary_key=True)
    ticker = models.CharField(max_length=100, unique=True)
    current_price = models.FloatField(null=True)
    name = models.CharField(max_length=255, null=True)
    summary = models.TextField(null=True)
    sector = models.CharField(max_length=100, null=True)
    dividends = models.ArrayField(model_container=Dividend, null=True)
    earnings = models.ArrayField(model_container=Earning, null=True)
    last_updated_time = models.DateTimeField(null=True, auto_now=True)

    objects = models.DjongoManager()

The worst part is running the exact same code from my test script works! and the database now has exactly 1 StockInfo object:

import json, time, yfinance

import django

from dividends_info.functions.stock_info import save_stock_info_data
from dividends_info.models import StockInfo

import sys

ticker = sys.argv[1]

def update_a_stock(ticker):
    stock, created = StockInfo.objects.get_or_create(ticker=ticker)
    # yahoo_stock_obj = yfinance.Ticker(ticker.upper())
    # earnings_history = yahoo_stock_obj.earnings_history
    # # dividends = yahoo_obj.dividends
    # save_stock_info_data(yahoo_stock_obj, ticker, stock, earnings_history)
    # print(earnings_history)
    # print(stock.dividends[-10:])
    # print(stock.earnings[:10])
    # # if not stock.earnings:
    # #     print(f"No earnings for {ticker}")
    # #     print(stock.earnings)
    # #     # time.sleep(2)
    # #     # print(earnings_history)
    # #     # time.sleep(5)
    # # else:
    # #     print(f"stock earnings length: {len(stock.earnings)}")
    # time.sleep(3)

if __name__ == '__main__':

I don't remember making any breaking changes and my view used to work for weeks now. How can I get the get_or_create to just return an empty StockInfo object?

Back to Top