#How to import many rows of data into django models.

2 messages · Page 1 of 1 (latest)

young sequoia
#

I don't want to click every time to add new record into models.
How to do it by script? I have model Stations where i have Id and Name
I want to import some Stations like
'Berlin'
'Paris'
'Barcelona' etc.

hazy hatch
#

Hello! You can programmatically create Django models using YourModel.objects.create() or its bulk_create() counterpart.

If you already have a list of IDs and Names, you might create a script that reads your original data and creates the objects.

Take note that bulk_create() behaves somewhat differently from regular creation.
https://docs.djangoproject.com/en/5.0/ref/models/querysets/#bulk-create

I recommend reading the documentation on Django Models as well.