Created
September 20, 2024 06:49
-
-
Save roderik333/0343f54af1e27dcbd50affb19919ac11 to your computer and use it in GitHub Desktop.
Map a pandas dataframe to django model
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dataframe_to_django( | |
df: Series | Any, | |
model: type[models.Model], | |
dbmapping: dict[str, str | None], | |
convmapping: dict[str, Any], | |
replace: bool = False, | |
) -> None: | |
"""Uses bulk_create to insert data in table. | |
KeywordArguments: | |
df -- the dataframe. | |
model -- the django table in to which the dataframe should be inserted. | |
dbmapping -- mapping from dataframe name to db name. Typically {"ProductName": "product_name", "SkuDescription": None, ...} | |
convmapping -- converters for spesific fields. Typically, {"StartDate": pd.to_datetime, ...} . | |
replace -- bool False|True. If set to True it will wipe the table before import. | |
""" | |
if replace: | |
model.objects.all().delete() | |
df.replace([np.nan], ["N/A"], inplace=True) | |
for key in convmapping: | |
df[key] = convmapping[key](df[key]) | |
model_list: list[models.Model] = [ | |
model(**{dbmapping[k]: v for k, v in x.items() if dbmapping[k] is not None}) for x in df.to_dict("records") | |
] | |
model.objects.bulk_create(model_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment