Skip to content

Commit bc0e4e5

Browse files
committed
Tweaked the makefixture command to not recurse on reverse relationships.
- Legacy-Id: 6446
1 parent 6f6f1d6 commit bc0e4e5

1 file changed

Lines changed: 19 additions & 15 deletions

File tree

ietf/utils/management/commands/makefixture.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
from django.db.models.fields.related import ManyToManyField
4848
from django.db.models.loading import get_models
4949

50-
DEBUG = False
50+
DEBUG = True
5151

5252
def model_name(m):
5353
module = m.__module__.split('.')[:-1] # remove .models
@@ -116,34 +116,38 @@ def handle_models(self, models, **options):
116116
raise CommandError("Wrong slice: %s" % slice)
117117

118118
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)
119131
if propagate:
120-
collected = set([(x.__class__, x.pk) for x in all])
121132
while objects:
122133
related = []
123-
for x in objects:
134+
for obj in objects:
124135
if DEBUG:
125-
print "Adding %s[%s]" % (model_name(x), x.pk)
136+
print "Adding %s[%s]" % (model_name(obj), obj.pk)
126137
# 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:
128139
if isinstance(f, ForeignKey):
129-
new = getattr(x, f.name) # instantiate object
140+
new = getattr(obj, f.name) # instantiate object
130141
if new and not (new.__class__, new.pk) in collected:
131142
collected.add((new.__class__, new.pk))
132143
related.append(new)
133144
if isinstance(f, ManyToManyField):
134-
for new in getattr(x, f.name).all():
145+
for new in getattr(obj, f.name).all():
135146
if new and not (new.__class__, new.pk) in collected:
136147
collected.add((new.__class__, new.pk))
137148
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)
145149
objects = related
146-
all.extend(objects)
150+
all.extend(related)
147151

148152
try:
149153
return serializers.serialize(format, all, indent=indent)

0 commit comments

Comments
 (0)