1

同じモデルで親子関係を持っています。例:

  • 親のコメント
    • 子コメント01
      • 子コメント02

すべての子スレッドを入れ子にできる API を構築したいと考えています。現在、親コメントのみが表示されます。

私の現在の API.py は次のようになります。

class ThreadResource(ModelResource):
    locations = fields.ToManyField('forum.api.comments','parent', full=True)

class Meta:
    queryset = comments.objects.all()
    resource_name = 'Comments'

class comments(ModelResource):
    class Meta:
        queryset = comments.objects.all()
        resource_name = 'comms'

モデルでそれを行った方法は次のとおりです。

class comments(models.Model):
    title = models.CharField(max_length=255)
    parent = models.ForeignKey('self', blank=True,null=True)
    sort = models.IntegerField(default=0)
    content = models.CharField(max_length=255)
4

1 に答える 1

3

まず、親コメントのクエリセットを返すフィルター関数を定義する必要があります。これを filter_comments_per_bundle と呼びましょう:

def filter_comments_per_bundle(bundle);
    parent = bundle.obj
    return comments.objects.filter(parent=parent)

次に、コメント モデル リソースに self への参照を追加します。

children = fileds.ToManyField('self', filter_comments_per_bundle, full = True, null = True)

最後に、申し訳ありませんが、それはペットピーブです。s/comments/Comment/ モデルは単数形で、最初の文字は大文字にする必要があります。

ああ、そして別のこと。Models と ModelResources に同じ名前を付けないでください。コメントの名前を ModelResource に変更します。

于 2013-08-26T17:51:46.013 に答える