Friday, September 8, 2017

Re: How to pre-load model data?

Mark,

It depends a bit on where you're getting your data from. One of the safest ways would to build a fixture, and use "manage.py loaddata". The fixture can be in json, xml or yaml formats.

If it's in some other format, then there are other ways of course too. There are always a few different ways you can anything I think. But the way I went about preloading from a CSV source once is I just wrote an importer. Really small python function that just reads the CSV and uses the django ORM to inject it into the database ...

I'm not sure what the advantage of those SQL methods is over using the ORM itself mind you. I wouldn't go there (or better said,  haven't yet ;-).

Regards,

Bernd.

Mark Phillips 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!

Mark
--
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/CAEqej2NDE8gqBEoSym1gA5DLD7Ygjo6MbN66mdwWDQ_Xd0%3DudA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


No comments:

Post a Comment