Changes between Version 15 and Version 16 of Fixtures


Ignore:
Timestamp:
Feb 24, 2021, 12:58:52 PM (3 years ago)
Author:
Elena Williams
Comment:

Removing outdated (erroneous) docs

Legend:

Unmodified
Added
Removed
Modified
  • Fixtures

    v15 v16  
    11= Fixtures =
    22
    3 Deprecated, use https://docs.djangoproject.com/en/2.2/howto/initial-data/
    4 
    5 ''This is a very simple example. Please read something more complex if you need.''
    6 
    7 Fixtures are very powerful to play with your database sample data during development process.
    8 After each '''python manage.py flush <myapp>''' command you need to populate database with sample data again and again using admin interface. It's quite boring, isn't it?
    9 With fixtures our life became more comfortable and easy. Look at this example.
    10 Lets imagine that you have some data in db. We can dump it, even if your models have ForeignKeys or any kind of *To* relations.
    11 
    12 First we need to define fixtures dir in settings file:
    13 
    14 {{{
    15 FIXTURE_DIRS = (
    16    '/path/to/myapp/fixtures/',
    17 )
    18 }}}
    19 
    20 Lets dump our data:
    21 
    22 {{{
    23 cd /path/to/my_project
    24 python manage.py dumpdata --format=json myapp > /path/to/myapp/fixtures/initial_data.json
    25 }}}
    26 
    27 Reset:
    28 
    29 {{{
    30 python manage.py reset myapp
    31 
    32 You have requested a database reset.
    33 This will IRREVERSIBLY DESTROY any data for
    34 the "myapp" application in the database "mydb".
    35 Are you sure you want to do this?
    36 
    37 Type 'yes' to continue, or 'no' to cancel: yes
    38 }}}
    39 
    40 Now we have clean DB, lets populate it with our sample data:
    41 
    42 {{{
    43 python manage.py syncdb
    44 Loading 'initial_data' fixtures...
    45 Installing json fixture 'initial_data' from '/path/to/myapp/fixtures/'.
    46 Installed 24 object(s) from 1 fixture(s)
    47 }}}
    48 
    49 == Fixture loading ==
    50 
    51 The location where Django loads a fixture from might seem unintuitive. As with template files, the fixtures of all applications in a project share the same namespace. If you follow [source:django/trunk/django/core/management/commands/loaddata.py?rev=9770#L79 loaddata.py] you see that Django searches for {{{ *appnames*/fixtures }}} and {{{ settings.FIXTURE_DIRS }}}  and loads the first match. So if you use names like {{{ testdata.json }}} for your fixtures you must make sure that no other active application uses a fixture with the same name. If not, you can never be sure what fixtures you actually load.
    52 
    53 Therefore it is suggested that you qualify your fixtures with the name of the associated application.  One strategy for this is to use the application name as a filename prefix, as in {{{myapp/fixtures/myapp_testdata.json}}}.  Another strategy, which is consistent with that recommended for templates and static files in the Django documentation, is to put your application fixtures in a application-named subdirectory, as in {{{myapp/fixtures/myapp/testdata.json}}}.  Both of these conventions work well with {{{loaddata}}}.
     3Deprecated, use https://docs.djangoproject.com/en/3.2/howto/initial-data/
    544
    555
    566Links: \\
    577[http://en.wikipedia.org/wiki/JSON] \\
    58 [https://docs.djangoproject.com/en/2.1/howto/initial-data/] \\
    59 [https://docs.djangoproject.com/en/2.1/topics/testing/tools/#fixture-loading] \\
    60 [https://docs.djangoproject.com/en/2.1/intro/tutorial03/] \\
    61 [https://docs.djangoproject.com/en/2.1/howto/static-files/]
     8[https://docs.djangoproject.com/en/3.2/howto/initial-data/] \\
     9[https://docs.djangoproject.com/en/3.2/topics/testing/tools/#fixture-loading] \\
     10[https://docs.djangoproject.com/en/3.2/intro/tutorial02/] \\
     11[https://docs.djangoproject.com/en/3.2/howto/static-files/]
Back to Top