django-test-utils makefixture コマンドの一般的な関係と 1 対 1 の関係のサポートを追加しようとしています。 /管理/コマンド/makefixture.py
誰かがこれを行う方法についてアイデアを持っていますか? または、次のような別のツールがあるかもしれません。
./manage.py dumpcmd User[:10] > fixtures.json
django-test-utils makefixture コマンドの一般的な関係と 1 対 1 の関係のサポートを追加しようとしています。 /管理/コマンド/makefixture.py
誰かがこれを行う方法についてアイデアを持っていますか? または、次のような別のツールがあるかもしれません。
./manage.py dumpcmd User[:10] > fixtures.json
問題にアプローチする方法はいくつかあります。django の内部をいじってからしばらく経っているので、コードを突くアプローチに集中します。
リンクから以下の関連コードを含めました。無関係な部分を削除したことに注意してください。また、 YOUR CASE HEREを編集する部分にはリファクタリングが必要であることに注意してください。
満足するまで、次のアルゴリズムに従ってください。
if
フィールドに応じてステートメントを (1 つ以上の) 個別の関数にリファクタリングします。テスト。
def handle_models(self, models, **options):
# SNIP handle options
all = objects
if propagate:
collected = set([(x.__class__, x.pk) for x in all])
while objects:
related = []
for x in objects:
if DEBUG:
print "Adding %s[%s]" % (model_name(x), x.pk)
# follow forward relation fields
for f in x.__class__._meta.fields + x.__class__._meta.many_to_many:
# YOU CASE HERE
if isinstance(f, ForeignKey):
new = getattr(x, f.name) # instantiate object
if new and not (new.__class__, new.pk) in collected:
collected.add((new.__class__, new.pk))
related.append(new)
if isinstance(f, ManyToManyField):
for new in getattr(x, f.name).all():
if new and not (new.__class__, new.pk) in collected:
collected.add((new.__class__, new.pk))
related.append(new)
# SNIP
objects = related
all.extend(objects)
# SNIP serialization