0

I could use some help figuring out how to debug this:

I suspect that there is something wrong with my models.py file but the error messages are pretty vague.

Using Alembic and sqlalchemy instead of Django ORM (relatively new to all the above) and successfully made a migration and migrated it to alembic version folder. Whole reason I'm using alembic sql alchemy is because I am trying to hit external api and was told in another post that alembic was needed to manage migration histories or there would be issues with django tracking migrations.

I have a custom command class that calls the scraper and I am passing the data to pandas dataframe then attempt to write it to database defined in my models.py file. For brevity I will just post my models.py, the end of the error log as well as link to full repo

from datetime import datetime
from sqlalchemy import Column, Integer, DateTime, String, Numeric, BigInteger, UniqueConstraint
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
from django.db import models

class CMC(Base):
   __tablename__ = 'apis_cmc'

   id = Column(Integer, primary_key=True)
   inserted_at = Column(DateTime, default=datetime.utcnow)
   name = Column(String)
   symbol = Column(String)
   price = Column(Numeric)
   market_cap = Column(BigInteger)
   market_cap_dominance = Column(BigInteger)
   fully_diluted_market_cap = Column(BigInteger)
   percent_change_1h = Column(Numeric)
   percent_change_24h = Column(Numeric)
   percent_change_30d = Column(Numeric)
   percent_change_60d = Column(Numeric)
   percent_change_7d = Column(Numeric)
   percent_change_90d = Column(Numeric)
   volume_24h = Column(Numeric)
   volume_change_24h = Column(Numeric)

   UniqueConstraint('symbol', 'inserted_at', name='uix_1')

   def __str__(self):
        return self.name
File "/Users/justinbenfit/Desktop/Programming/website/cds_website/venv/lib/python3.8/site-packages/django/contrib/admin/sites.py", line 116, in register
    for model in model_or_iterable:
TypeError: 'DeclarativeMeta' object is not iterable

https://github.com/Justinbenfit23/crypto_data_science_website

The github issues I have seen for this error seem to refer to adding an iter method to the model class which I don't think I have here so I'm stuck. Any direction appreciated. Thanks!

Justin Benfit
  • 423
  • 3
  • 11
  • 1
    I am confused why you need to use sqlalchemy. If you need to manage the external database (API) by performing migrations on it using alembic, it would be easier to ditch sqlalchemy/alembic and use django to manage the external database (API). – Jimmy Pells Feb 08 '22 at 17:07
  • @JimmyPells, Thank you for responding! I was under the impression when I first started the project that I needed to write the data from the api call to a dataframe in order to be able to insert it to db and I didn't think that django orm was able to parse a dataframe. I guess I probably need to figure out a way to have the custom command class run the api call then feed django the data in a way it can ingest it. – Justin Benfit Feb 08 '22 at 17:17
  • 1
    Usually developers will not use 2 different ORMs (Django vs. sqlalchemy) in the same application. I presume you are querying an API and importing data into a database that you control. The way you are querying this API sounds like you are loading the data into a Pandas DataFrame which does provide a method to insert into sqlalchemy. If the benefits of using Pandas and sqlalchemy outweigh the benefits of Django you might consider using Flask instead, but I suspect you are going to find it easier to query your API and import it using Django's ORM. – Jimmy Pells Feb 08 '22 at 17:39
  • 1
    This may be a helpful tutorial for you explaining how to retrieve data from an API and insert it into your database using Django's ORM https://medium.com/@chilinski.a/how-to-seed-a-django-api-with-data-from-an-external-api-b577b6e6ad54 – Jimmy Pells Feb 08 '22 at 17:44
  • 1
    Thank you! I am thinking that I just need to figure out a way to convert the response into json as I think django prefers that. I will check out the article. Thanks again. This has been very helpful! – Justin Benfit Feb 08 '22 at 18:59

0 Answers0