Skip to content

Commit

Permalink
Merge pull request #3 from solarmonkey/add-flexible-dumps
Browse files Browse the repository at this point in the history
Add flexible dumps
  • Loading branch information
tino authored Aug 3, 2018
2 parents c32e880 + a4ee230 commit bd0128f
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 6 deletions.
8 changes: 6 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ a specific object as well as all its dependencies (as defined by ForeignKeys).
## OR
./manage.py dump_object APP.MODEL --query '{"pk__in": [PK1, PK2, PK3]}' > my_new_fixture.json

Or you can get all objects with all dependencies by passing an asterisk:
Or you can get all objects with all dependencies by not specifying the pks:

./manage.py dump_object APP.MODEL '*' > my_new_fixture.json
./manage.py dump_object APP.MODEL > my_new_fixture.json

You can also dump objects via a manager method:

./manage.py dump_object APP.MODEL.METHOD > my_new_fixture.json

You can now safely load ``my_new_fixture.json`` in a test without foreign key i
errors.
Expand Down
15 changes: 11 additions & 4 deletions fixture_magic/management/commands/custom_dump.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Command(BaseCommand):

def add_arguments(self, parser):
parser.add_argument('dump_name')
parser.add_argument('pk', nargs='+')
parser.add_argument('pk', nargs='*')
parser.add_argument('--natural', default=False, action='store_true', dest='natural',
help='Use natural keys if they are available.')

Expand All @@ -38,10 +38,17 @@ def handle(self, *args, **options):
dump_name = options['dump_name']
pks = options['pk']
dump_settings = settings.CUSTOM_DUMPS[dump_name]
(app_label, model_name) = dump_settings['primary'].split('.')
app_label, model_name, *manager_method = dump_settings['primary'].split('.')
include_primary = dump_settings.get("include_primary", False)
dump_me = loading.get_model(app_label, model_name)
objs = dump_me.objects.filter(pk__in=[int(i) for i in pks])
default_manager = loading.get_model(app_label, model_name).objects
if manager_method:
dump_me = getattr(default_manager, manager_method[0])()
else:
dump_me = default_manager
if pks:
objs = dump_me.filter(pk__in=pks)
else:
objs = dump_me
for obj in objs.all():
# get the dependent objects and add to serialize list
for dep in dump_settings['dependents']:
Expand Down

0 comments on commit bd0128f

Please sign in to comment.