0

I am new to Django and have read the basic and advanced tutorial along with some parts of the documentation on migrations and data migrations: https://docs.djangoproject.com/en/4.0/topics/migrations/#data-migrations

Also, I have read about fixtures but this seems to be suitable only for initial data provision: https://docs.djangoproject.com/en/4.0/howto/initial-data/#providing-data-with-fixtures

The task I want to accomplish is to bulk insert data repeatedly from "outside" in a clean way using using Djangos ORM models and all the benefits of migrations using a command line or other suitable interface such as a REST API.

So far, I haven't found a full example on how to do this because using the first option of data migration would require to touch a file everytime I insert/migrate new data.

Any hints on how to accomplish this or am I missing something?

Thanks in advance!

8
  • Are you asking how to create a migration to insert data? Migrations are generally run once, what sure what you mean by "insert data repeatedly"? Commented Jan 28, 2022 at 14:39
  • Yes, I want to insert data periodically using Djangos ORM but not using "classical" browser requests with views. I was rather hoping for a possibility using Djangos CLI or an API and have specified my answer accordingly. Commented Jan 28, 2022 at 14:57
  • The problem is that the amount of data can be quite large and want to circumvent problems like timeouts, etc. Commented Jan 28, 2022 at 15:01
  • You are talking about calling an external API and inserting the data returned? This is nothing like a fixture or migration, you probably need a custom management command. Can you add your model and an example call/response from the API? Commented Jan 28, 2022 at 15:02
  • 1
    You are right. This perfectly solves my problem. I just would not have searched this funcionality using these keywords :) Thanks a lot for your answers! Commented Jan 28, 2022 at 19:47

2 Answers 2

1

I think you are missing the point of migrations. Use them if you want to alter a schema on your database, or if you want to insert some data once. For your case, you should use management commands.

Sign up to request clarification or add additional context in comments.

1 Comment

You are right. Management commands are exactly what I was looking for and I was already wondering if every insert really needs a data migration. Thanks a lot!
0

You can create an empty migration with

python manage.py makemigrations --empty yourappname

Then define some functions with the data you want to fill like this:

from django.db import migrations

def create_estado(apps, schema_monitor):
    '''
    Create estado of empaque
    '''
    Estado = apps.get_model('empaques_app', 'Estado_empaque')

    Estado.objects.create(nombre='Bueno')
    Estado.objects.create(nombre='Dañado')
    Estado.objects.create(nombre='Reparando')

class Migration(migrations.Migration):

    dependencies = [
        ('empaques_app', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(create_estado)
    ]

1 Comment

But this would be only for one insert, right? I am looking for a solution to repeatedly insert data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.