139 lines
3.0 KiB
Plaintext
139 lines
3.0 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"## This is a prototype way to create models on the fly using pandas as the type parser.\n",
|
|
"### It was phased out in favor of creating models manually to avoid any mistakes pandas could make. \n",
|
|
"### However it is kept here for just in case."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Automatically create the django model using pandas for type inference"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import pandas as pd\n",
|
|
"from django.db import models"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"read the csv file"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"df = pd.read_csv(\"GaiaSource_000000-003111.csv\", comment='#')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"map pandas types to django fields"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"dtype_mapping = {\n",
|
|
" 'int64' : 'models.IntegerField(null=True)',\n",
|
|
" 'float64' : 'models.FloatField(null=True)',\n",
|
|
" 'bool' : 'models.BooleanField(null=True)',\n",
|
|
" 'object' : 'models.CharField(max_length=255, null=True)'\n",
|
|
"}"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"generate the model code line by line"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def generate_model(df, model_name):\n",
|
|
"\n",
|
|
" class_code = f\"class {model_name}(models.Model):\\n\"\n",
|
|
"\n",
|
|
" for column in df.columns:\n",
|
|
" dtype = str(df[column].dtype)\n",
|
|
" field_type = dtype_mapping.get(dtype, 'models.CharField(max_length=255, null=True)')\n",
|
|
" class_code += f\" {column} = {field_type}\\n\"\n",
|
|
" return class_code\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"model_code = generate_model(df, 'samplemodel')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"save the model code to the models.py file to be used"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"with open('gaia_orm/sample_app/models.py', 'a') as file:\n",
|
|
" file.write(model_code)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "venv",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.12.5"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|