Changes between Initial Version and Version 1 of Fixtures


Ignore:
Timestamp:
Oct 7, 2007, 3:14:28 AM (17 years ago)
Author:
Lolka <alexanderad@…>
Comment:

Very simple example of fixtures

Legend:

Unmodified
Added
Removed
Modified
  • Fixtures

    v1 v1  
     1''It is very simple example, please, read something more complex if you need.''
     2
     3Fixtures are very powerful to play with your database sample data during development process.
     4After each '''python manage.py reset <myapp>''' command you need to populate database with sample data again and again using admin interface. It's quite boring, isn't it?
     5With fixtures our life became more comfortable and easy. Look at this example.
     6Lets imagine that you have some data in db, so we can dump it. Even if your models have ForeignKeys or any kind of *To* relations.
     7
     8First we need to define fixtures dir in settings file:
     9
     10{{{
     11FIXTURE_DIRS = (
     12   '/path/to/myapp/fixtures/',
     13)
     14}}}
     15
     16Lets dump our data:
     17
     18{{{
     19cd /path/to/myapp/fixtures
     20python manage.py dumpdata --format=json myapp > initial_data.json
     21}}}
     22
     23Reset:
     24
     25{{{
     26python manage.py reset myapp
     27
     28You have requested a database reset.
     29This will IRREVERSIBLY DESTROY any data for
     30the "myapp" application in the database "mydb".
     31Are you sure you want to do this?
     32
     33Type 'yes' to continue, or 'no' to cancel: yes
     34}}}
     35
     36Now we have clean DB, lets populate it with our sample data:
     37
     38{{{
     39python manage.py syncdb
     40Loading 'initial_data' fixtures...
     41Installing json fixture 'initial_data' from '/path/to/myapp/fixtures/'.
     42Installed 24 object(s) from 1 fixture(s)
     43}}}
Back to Top