[melbourne-pug] Django: Best practice for importing data (was: Specify the database for a Django ModelForm instance)
Ben Finney
ben+python at benfinney.id.au
Tue Aug 9 00:50:21 EDT 2016
Mike Dewhirst <miked at dewhirst.com.au> writes:
> Can I ask why you are using manage.py to import csv data instead of a
> migration?
There are different scenarios when each makes sense.
* When the data import is conceptually a defining feature of the
database (e.g. a collection of status values), and should exist from
the first creation of the database, storing the data as a fixture is
the best way.
<URL:https://docs.djangoproject.com/en/1.10/howto/initial-data/>
Use an initial data fixture when your database is just starting, and
you know this data should exist in every new instance of the
application.
* When the change in data is reflective of a change in the application
behaviour (e.g. the application was only offering Country and State,
but now needs to also offer City values), populating the new
information with a migration is the right: the data should be there
whenever the application behaviour aligns with that database state,
and should not be there otherwise.
<URL:https://docs.djangoproject.com/en/1.10/topics/migrations/#data-migrations>
Use a migration when the change in data is required for every instance
of the application which exists at that state of the program code.
* When the change in data does not represent any specific change in
application behaviour (e.g. the application offered 135
manually-entered products for sale, and now the owner wants to import
another 6 000 additional products), a migration is too much hassle
because there's no implied different behaviour of the application.
<URL:https://docs.djangoproject.com/en/1.10/howto/custom-management-commands/>
For small data additions, that do not need to be applied in every
instance of the application, you could just use the application's user
interface – or the Admin – to create them. But if the amount is large,
a custom management command is best for automating it.
> I ask because I'm just starting to think about an upcoming csv (xlsx
> actually) import task of my own.
I hope that helps.
--
\ “I was in the first submarine. Instead of a periscope, they had |
`\ a kaleidoscope. ‘We're surrounded.’” —Steven Wright |
_o__) |
Ben Finney
More information about the melbourne-pug
mailing list