Friday, September 8, 2017

Re: How to pre-load model data?



On Sep 7, 2017 9:57 PM, "Mark Phillips" <mark@phillipsmarketing.biz> wrote:
I have a several classes in models.py, and I would like to load some data into these models (or tables in the db), as well as add more data over time through the admin panel. 

I googled for some ideas on how to pre-populate the db tables, and found references to RunSQL  (https://docs.djangoproject.com/en/1.11/ref/migration-operations/#django.db.migrations.operations.RunSQL) to execute some SQL commands during a migration (http://blog.endpoint.com/2016/09/executing-custom-sql-in-django-migration.html). 

Is this the "correct" way to accomplish my goal of populating the data tables with some data, or is there another way the more closely ties the data to the models?

Thanks!

Possibly, although for purely adding data that can be coerced through existing models, RunSQL isn't your best choice.

Is this data something that needs to be inserted repeatedly across multiple installations, or is it data used specifically by a single installed instance? Would the data added in the future rely on the data you are currently inserting? 

If these are one-off data loads, I would look at creating a manage.py management command to parse the data and insert it in to the Django database using model forms. At a high level, you would deserialize the data from your source, feed it to a form, and use the form to save that row of data. I suppose you could use a formset to validate/save multiple rows in a single action, but error handling become problematic and isn't any more efficient on the SQL side.

Form classes offer the benefit of validation of data in addition to saving the model instance. This also means that the external data would be validated the same way that an end-user entry would be, if that is important to you. Model forms also handle any M2M relationships transparently.

If you are 150% sure that the data you have is clean, then you may be able to jump straight in to the ORM. 



If this is data that you are passing along to those who install your app, a migration is the way to go. You might even mix the two approaches and create the management command, then simply run the management command from the migration. That strategy would be the most extensible if the command can be run manually for other data provided by the customer.

-James

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2Be%2BciU5ZidkqOZttR2VyRtdzYDdAnhJ0-Mb4ALTsYWKRGY%2BdA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment