Skip to content

Loading…

Added support for multiple ids/pks in custom dump primary object #18

Merged
merged 1 commit into from

2 participants

@pratyushmittal

I have found custom dump very useful, however it did not support multiple primary objects (as dump_object does).

Thus I have added support for multiple ids in custom_dump as similar to dump_object

@davedash davedash merged commit 138130c into davedash:master
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Showing with 14 additions and 14 deletions.
  1. +14 −14 fixture_magic/management/commands/custom_dump.py
View
28 fixture_magic/management/commands/custom_dump.py
@@ -13,27 +13,27 @@
from fixture_magic.utils import (add_to_serialize_list, reorder_json,
serialize_me, serialize_fully)
-
class Command(BaseCommand):
help = 'Dump multiple pre-defined sets of objects into a JSON fixture.'
- args = "[dump_name pk]"
+ args = "[dump_name pk [pk2 pk3 [..]]"
- def handle(self, dump_name, pk, **options):
+ def handle(self, dump_name, *pks, **options):
# Get the primary object
dump_settings = settings.CUSTOM_DUMPS[dump_name]
(app_label, model_name) = dump_settings['primary'].split('.')
dump_me = loading.get_model(app_label, model_name)
- obj = dump_me.objects.get(pk=pk)
- # get the dependent objects and add to serialize list
- for dep in dump_settings['dependents']:
- try:
- thing = Variable("thing.%s" % dep).resolve({'thing': obj})
- add_to_serialize_list([thing])
- except VariableDoesNotExist:
- sys.stderr.write('%s not found' % dep)
-
- if not dump_settings['dependents']:
- add_to_serialize_list([obj])
+ objs = dump_me.objects.filter(pk__in=[int(i) for i in pks])
+ for obj in objs:
+ # get the dependent objects and add to serialize list
+ for dep in dump_settings['dependents']:
+ try:
+ thing = Variable("thing.%s" % dep).resolve({'thing': obj})
+ add_to_serialize_list([thing])
+ except VariableDoesNotExist:
+ sys.stderr.write('%s not found' % dep)
+
+ if not dump_settings['dependents']:
+ add_to_serialize_list([obj])
serialize_fully()
data = serialize('json', [o for o in serialize_me if o is not None])
Something went wrong with that request. Please try again.