|
47 | 47 | from django.db.models.fields.related import ManyToManyField |
48 | 48 | from django.db.models.loading import get_models |
49 | 49 |
|
50 | | -DEBUG = False |
| 50 | +DEBUG = True |
51 | 51 |
|
52 | 52 | def model_name(m): |
53 | 53 | module = m.__module__.split('.')[:-1] # remove .models |
@@ -116,34 +116,38 @@ def handle_models(self, models, **options): |
116 | 116 | raise CommandError("Wrong slice: %s" % slice) |
117 | 117 |
|
118 | 118 | all = objects |
| 119 | + collected = set([(x.__class__, x.pk) for x in all]) |
| 120 | + related = [] |
| 121 | + for obj in objects: |
| 122 | + # follow reverse relations as requested |
| 123 | + for reverse_field in follow_reverse.get(obj.__class__, []): |
| 124 | + mgr = getattr(obj, reverse_field) |
| 125 | + for new in mgr.all(): |
| 126 | + if new and not (new.__class__, new.pk) in collected: |
| 127 | + collected.add((new.__class__, new.pk)) |
| 128 | + related.append(new) |
| 129 | + objects += related |
| 130 | + all.extend(related) |
119 | 131 | if propagate: |
120 | | - collected = set([(x.__class__, x.pk) for x in all]) |
121 | 132 | while objects: |
122 | 133 | related = [] |
123 | | - for x in objects: |
| 134 | + for obj in objects: |
124 | 135 | if DEBUG: |
125 | | - print "Adding %s[%s]" % (model_name(x), x.pk) |
| 136 | + print "Adding %s[%s]" % (model_name(obj), obj.pk) |
126 | 137 | # follow forward relation fields |
127 | | - for f in x.__class__._meta.fields + x.__class__._meta.many_to_many: |
| 138 | + for f in obj.__class__._meta.fields + obj.__class__._meta.many_to_many: |
128 | 139 | if isinstance(f, ForeignKey): |
129 | | - new = getattr(x, f.name) # instantiate object |
| 140 | + new = getattr(obj, f.name) # instantiate object |
130 | 141 | if new and not (new.__class__, new.pk) in collected: |
131 | 142 | collected.add((new.__class__, new.pk)) |
132 | 143 | related.append(new) |
133 | 144 | if isinstance(f, ManyToManyField): |
134 | | - for new in getattr(x, f.name).all(): |
| 145 | + for new in getattr(obj, f.name).all(): |
135 | 146 | if new and not (new.__class__, new.pk) in collected: |
136 | 147 | collected.add((new.__class__, new.pk)) |
137 | 148 | related.append(new) |
138 | | - # follow reverse relations as requested |
139 | | - for reverse_field in follow_reverse.get(x.__class__, []): |
140 | | - mgr = getattr(x, reverse_field) |
141 | | - for new in mgr.all(): |
142 | | - if new and not (new.__class__, new.pk) in collected: |
143 | | - collected.add((new.__class__, new.pk)) |
144 | | - related.append(new) |
145 | 149 | objects = related |
146 | | - all.extend(objects) |
| 150 | + all.extend(related) |
147 | 151 |
|
148 | 152 | try: |
149 | 153 | return serializers.serialize(format, all, indent=indent) |
|
0 commit comments