7

django-test-utils makefixture コマンドの一般的な関係と 1 対 1 の関係のサポートを追加しようとしています。 /管理/コマンド/makefixture.py

誰かがこれを行う方法についてアイデアを持っていますか? または、次のような別のツールがあるかもしれません。

./manage.py dumpcmd User[:10] > fixtures.json
4

1 に答える 1

1

問題にアプローチする方法はいくつかあります。django の内部をいじってからしばらく経っているので、コードを突くアプローチに集中します。

リンクから以下の関連コードを含めました。無関係な部分を削除したことに注意してください。また、 YOUR CASE HEREを編集する部分にはリファクタリングが必要であることに注意してください。

満足するまで、次のアルゴリズムに従ってください。

  1. ifフィールドに応じてステートメントを (1 つ以上の) 個別の関数にリファクタリングします。
  2. どのフィールドが一般的な関係に対応するかがわかるまで、インスペクション コードを追加します。
  3. 一般的な関係に従うまで、抽出コードを追加します。
  4. テスト。

    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
    
于 2011-07-15T08:47:45.810 に答える