I am trying to insert data in a Pandas DataFrame into an existing Django model, Agency
, that uses a SQLite backend. However, following the answers on How to write a Pandas Dataframe to Django model and Saving a Pandas DataFrame to a Django Model leads to the whole SQLite table being replaced and breaking the Django code. Specifically, it is the Django auto-generated id
primary key column that is replaced by index
that causes the errors when rendering templates (no such column: agency.id
).
Here is the code and the result of using Pandas to_sql on the SQLite table, agency
.
In models.py
:
class Agency(models.Model):
name = models.CharField(max_length=128)
In myapp/management/commands/populate.py
:
class Command(BaseCommand):
def handle(self, *args, **options):
# Open ModelConnection
from django.conf import settings
database_name = settings.DATABASES['default']['NAME']
database_url = 'sqlite:///{}'.format(database_name)
engine = create_engine(database_url, echo=False)
# Insert data data
agencies = pd.DataFrame({"name": ["Agency 1", "Agency 2", "Agency 3"]})
agencies.to_sql("agency", con=engine, if_exists="replace")
Calling 'python manage.py populate
' successfully adds the three agencies into the table:
index name
0 Agency 1
1 Agency 2
2 Agency 3
However, doing so has changed the DDL of the table from:
CREATE TABLE "agency" ("id" integer NOT NULL PRIMARY KEY AUTOINCREMENT, "name" varchar(128) NOT NULL)
to:
CREATE TABLE agency (
"index" BIGINT,
name TEXT
);
CREATE INDEX ix_agency_index ON agency ("index")
How can I add the DataFrame to the model managed by Django and keep the Django ORM intact?